I'm running the command git diff-index main
in a repo that has the working tree equal to the branch main
, but changes cached in the index. The command tells me that there are differences (I understand that is between the working tree and the branch main
). But there are no difference.
First setup the repo
$ git init -b main /tmp/repo
Initialized empty Git repository in /tmp/repo/.git/
$ cd /tmp/repo
$ (main #) ls -AF
.git/
$ (main #) echo hello > world
$ (main #%) git add .
$ (main +) git commit -m hello
[main (root-commit) 14a90af] hello
1 file changed, 1 insertion(+)
create mode 100644 world
$ (main) echo bye > world
$ (main *) git add .
$ (main *+) echo hello > world
Verifying that the file world
is the same in main
and in the working tree
$ (main *+) git hash-object world
ce013625030ba8dba906f756967f9e9ca394464a
$ (main *+) git ls-tree -r main | grep world
100644 blob ce013625030ba8dba906f756967f9e9ca394464a world
But when running git diff-index main
, it tells me that there are differences
$ (main *+) git diff-index main
:100644 100644 ce013625030ba8dba906f756967f9e9ca394464a 0000000000000000000000000000000000000000 M world
Moreover, when running git diff-index -p main
, to see the differences, It show me nothing.
$ (main *+) git diff-index -p main
Maybe something I misunderstood about git diff-index
. I don't know if it's the expected behavior. Can someone explain me more about this behavior?
I think the important bits from the man page are these:
These commands all compare two sets of things; what is compared differs:
git-diff-index <tree-ish>
: compares the and the files on the filesystem. […][…] After that, all the commands print one output line per changed file.
An output line is formatted this way:
in-place edit :100644 100644 bcd1234 0123456 M file0 […]
That is, from the left to the right:
[…]
- sha1 for "dst"; 0{40} if deletion, unmerged or "work tree out of sync with the index".
[…]
The sha1 for "dst" is shown as all 0’s if a file on the filesystem is out of sync with the index.
Example:
:100644 100644 5be4a4a 0000000 M file.c
Since your file has the content "hello" in your working tree, but the content "bye" in the index, it's out of sync and a line with 00000… is printed.