2

My git repository got into a strange state today. A file was reported by git status as modified, but git diff showed no changes. After a searching on internet, I checked the file attributes (read/write/execute and owner GID/UID) and content (in case it was a line endings issue). The attributes were correctly set, and md5sum theFile and git show HEAD:theFile | md5sum reported the same hash.

I then tried to overwrite the working tree file with the one stored in git: git show HEAD:theFile > theFile. Surprisingly, git status continued to report changes on this file.

Then, I checked out the file: git checkout -- theFile, and after that, git status changed its opinion and reported no changes on theFile.

I can't understand the difference between show HEAD:theFile > theFile and checkout -- theFile. Maybe git is keeping information in cache, which is updated by checkout but not by show ... > ?

N3dst4
  • 6,360
  • 2
  • 20
  • 34
audeoudh
  • 1,279
  • 1
  • 8
  • 23

1 Answers1

1

As I detailed here:

git checkout <tree-ish> -- <pathspec>

When <paths> are given, git checkout does not switch branches.
It updates the named paths in the working tree from the index file or from a named <tree-ish> (most often a commit).

That means git checkout -- path will override the working tree with what has already been staged (git add'ed).

git checkout -- theFile makes the index and working tree identical, which means git diff is empty.

show HEAD:theFile > theFile does not modify the index, so a git diff would still report differences between the index and the working tree.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. This explain the difference between `checkout -- theFile` and `show HEAD:theFile > theFile`. However, if no modification is staged on this file (there are nothing in the index concerning it), I expect that making the working tree identical to the index or to `HEAD` produces a similar result, isn't it ? – audeoudh Jan 14 '16 at 12:49
  • @ryolait maybe there is a .gitattributes which concerns that file? – VonC Jan 14 '16 at 13:00
  • No. `find -name .gitattributes` returns nothing, and `.git/info/attributes` does not exists. – audeoudh Jan 14 '16 at 14:06