0

When using 'git ls-files -s' and 'git log' on the same file, I get different SHA hashes. Take the file lib/nerdtree/nerdtree.vim in repo https://github.com/preservim/nerdtree, tag 6.10.5, for example.

The command git log lib/nerdtree/nerdtree.vim produces,

commit 593c16add35a5461f189b8189abe219f7bbbd604 (tag: 6.10.5)

But the command git ls-files -s lib/nerdtree/nerdtree.vim produces,

100644 61a11a96ba44c7b1bf0472b598f2c967b2dce9f2 0 lib/nerdtree/nerdtree.vim

If I attempt to checkout the SHA returned by 'git log', that command succeeds. If I attempt to checkout the SHA returned by 'git ls-files -s', that produces a fatal error:

git checkout 61a11a96ba44c7b1bf0472b598f2c967b2dce9f2 lib/nerdtree/nerdtree.vim

fatal: reference is not a tree: 61a11a96ba44c7b1bf0472b598f2c967b2dce9f2

Why does 'git ls-files -s' and 'git log' produce different SHA hashes for the same file?

NOTE: I searched around for an answer and found this thread: Git - finding the SHA1 of an individual file in the index. This thread explains why there might be differences between the output of 'git hash-object' and 'git ls-files -s', but it does not explain the difference between the output of 'git ls-files -s' and 'git log'.

Michael
  • 497
  • 1
  • 5
  • 7
  • 2
    Read https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain for more information about the various objects that exist in a Git repository and how they are used to implement a source control system. – chepner Jun 02 '21 at 19:40
  • @chepner I didn't realize that there are file objects and commit objects in GIT. This helped me: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects – Michael Jun 04 '21 at 16:57
  • Sorry, that's the link I really meant to send. I didn't look at the table of contents closely enough to see there was a separate link to that section of chapter 10. Glad it helped! – chepner Jun 04 '21 at 16:59

2 Answers2

6

The reason is:

  1. The hash returned by git log is the hash which identifies the commit.
  2. The hash returned by git ls-files -s is the identifier of the file blob.
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
2

git log with a path lists commits that change what's recorded at that path.

git ls-files with a path lists what's recorded in your current checkout at that path.

jthill
  • 55,082
  • 5
  • 77
  • 137