On an untouched file, git thinks the file is removed then added back. I can't figure out why.
The symptoms, in terms of git, can be shown as follows:
$ git diff-index --cached HEAD
:000000 100644 0000000000000000000000000000000000000000 8d1b41aeb85f3c2afc6e70e824902f3f365e416c A myfile.h
:100644 000000 8d1b41aeb85f3c2afc6e70e824902f3f365e416c 0000000000000000000000000000000000000000 D myfile.h
This looks alien to me. The checksum of the file is the same, the mode of the file is the same, but git still thinks there is a change removing the file and then a change adding the file and this is between the index and the working directory !
git status says
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: myfile.h
But using git reset HEAD myfile.h changes nothing, nor does git checkout myfile.h. Removing the file physically and creating it again does not fix the problem either.
git reset --hard gets rid of the problem, but is not an option in many cases when this is happening to me like in the middle of a rebase.
In this situation I can also do the following :
$ git rm --cached myfile.h
rm 'myfile.h'
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: myfile.h
Untracked files:
(use "git add <file>..." to include in what will be committed)
myfile.h
$ git add myfile.h
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: myfile.h
$ git diff
$
I'd like to figure out why git thinks this file is removed and added, but I'm not sure any more in what direction to investigate. How can I ask git more info about what it thinks of my index so I can figure out why it wants so much to remove/add this unchanged file ?
UPDATE : I still haven't found the reason but I have more information to share. I've tried to compare the git tree objects referencing the case where git reports a difference and the case where it doesn't. It turns out that git ls-tree on the parent directory of the problematic file look different on one account: the order of the files.
100644 blob e79f590c93e9a971c9c443558f35067898d2587f myeee.h
100644 blob 8d1b41aeb85f3c2afc6e70e824902f3f365e416c myfile.h
040000 tree a9091c79f0d317f1d0ea596e538b50afde4b6251 myfile
100644 blob 47925e1b444f396a038052dc583310dc3a6b6021 myfile_again.h
versus
100644 blob e79f590c93e9a971c9c443558f35067898d2587f myeee.h
040000 tree a9091c79f0d317f1d0ea596e538b50afde4b6251 myfile
100644 blob 8d1b41aeb85f3c2afc6e70e824902f3f365e416c myfile.h
100644 blob 47925e1b444f396a038052dc583310dc3a6b6021 myfile_again.h
This seems to be the source of the difference git is seeing... but I still don't know why it sees something different.