How can I get SHA hash of a file in specified commit? I can get all commits that touched the file using git log file
, but how can I get SHA hash of a file in each particular commit?
I think I can do it by checking out the commit and than use git-hash-object
, but there must be easier way.
There is a very quick way to get the Git hash for a file within some commit:
git rev-parse <commit-ID>:/path/to/file
Git's hash is a SHA-1 of the word blob
followed by a space, followed by a decimal ASCII string giving the size of the file in bytes, followed by a NUL byte, followed by the file's contents:
size=$(wc -c $file)
(printf "blob %d\0" $size; cat $file) | sha1sum -
It looks from comments, though, like you want an actual SHA-1 of the file's contents (as someone else would get by extracting the file and running sha1sum
on it), and not the git hash:
git show <commit-ID>:path | sha1sum -
is the general (non-bash-specific) method (bash's <(
is fine as well, just make sure you have the fdesc file system mounted).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With