Michael
Michael

Reputation: 527

Why does 'git ls-files -s' and 'git log' output a different SHA hash?

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'.

Upvotes: 0

Views: 544

Answers (2)

Antonio Petricca
Antonio Petricca

Reputation: 10996

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.

Upvotes: 6

jthill
jthill

Reputation: 60235

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.

Upvotes: 2

Related Questions