Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SHA hash of file in specific commit

Tags:

git

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.

like image 969
graywolf Avatar asked Apr 26 '16 18:04

graywolf


1 Answers

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

like image 128
torek Avatar answered Oct 24 '22 10:10

torek