Why does git hash-object return a different hash than openssl sha1?

You see a difference because git hash-object doesn’t just take a hash of the bytes in the file – it prepends the string “blob ” followed by the file size and a NUL to the file’s contents before hashing. There are more details in this other answer on Stack Overflow:

  • How to assign a Git SHA1’s to a file without Git?

Or, to convince yourself, try something like:

$ echo -n hello | git hash-object --stdin
b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0

$ printf 'blob 5\0hello' > test.txt
$ openssl sha1 test.txt
SHA1(test.txt)= b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)