Search code examples
bashgitgit-bashsha

Git hash output does not match sha1sum, why?


I've seen this post, and it mentions that Git uses a SHA-1 hash for the version that I am using. However, when I'm comparing the output of git hash-object to the familiar sha1sum, I get different results. For example:

$ git --version
git version 2.17.1
$ git hash-object data/letter.txt
2e65efe2a145dda7ee51d1741299f848e5bf752e
$ sha1sum data/letter.txt
86f7e437faa5a7fce15d1ddcb9eaeaea377667b8  data/letter.txt

I am following this post. The file letter.txt contains a single letter 'a'.

Why would these commands produce different results?


Solution

  • The hashes do not match because Git prepends a header (blob #{content.length}\0) to the data/content (of the file).

    cf. https://git-scm.com/book/en/v2/Git-Internals-Git-Objects