0

Using Git, version 1.9.3, on Fedora, version 20, I repeatedly run into the problem that I cannot commit an empty bar file without making Git think that I deleted the previously versioned, non-empty foo file. Both files happen to live in the same folder. My commands to add and commit the empty bar file look like:

ls
# bar foo

file bar foo
# bar: empty
# foo: ASCII text

git status --short -- .
# ?? bar

git add --intent-to-add --verbose bar
# add 'bar'

git diff --cached -- .
# diff --git c/bar i/bar
# new file mode 100644
# index 0000000..e69de29

git commit --message=bar
# [master 1234567] bar
1 file changed, 44 deletions(-)
delete mode 100644 foo

git status --short -- .
# AM bar
# A  foo

file bar foo
# bar: empty
# foo: ASCII text

Have you got an explanation for Git's behavior that I observe? Any solution?

Tim Friske
  • 2,012
  • 1
  • 18
  • 28
  • It is a habit of mine to start with an empty file, building its content up what results in many small commits for this file. – Tim Friske Jun 24 '14 at 21:41
  • 1
    Do you have any hooks? Git aliases? Shell aliases? – Edward Thomson Jun 24 '14 at 21:54
  • One "post-commit" hook and lots of Git aliases. But I was able to reproduce the problem with all my Git aliases translated to the above commands. The hook is only creating a Git note in a ref other than the "commits" standard. So *no* rewriting of a message or even the commit itself. – Tim Friske Jun 24 '14 at 22:38
  • 1
    Not meant as a full answer: don't use `--intent-to-add`, it doesn't do what you want and is messing with your sequence. – torek Jun 24 '14 at 22:38

1 Answers1

0

I misconceived git add --intend-to-add -- bar alone to add an empty entry to the index. Git allows me to commit the index in such a state resulting in an empty commit as if I had written git add --allow-empty. But an empty commit is cleary what I do not want. What I want instead corresponds to the following sequence of commands:

echo foo > bar
git add --intent-to-add -- bar
git add --patch -- bar
# Edit (e) the hunk to be staged and make it empty.
git commit --message 'add empty bar file.'
git log --oneline --name-status -1
# 1234567 add empty bar file
# A bar

The command that I was missing is git add --patch -- bar. Without it A bar would not have been printed at the end.

Tim Friske
  • 2,012
  • 1
  • 18
  • 28