Alex I Alex I - 2 months ago 6x
Git Question

How to find file revision or commit from git repository by a file hash?

I have a git diff output that contains lines like this:

index 0056c73..92c6cbd 100644
for each file. I know which repository it comes from, but unfortunately have no idea which revision of the repository it is diffing against.

How do I find the commit of the repository that the diff is against?

Alternately, how do I find the exact commit of each file that the pre-image hashes in the diff correspond to? (i.e. which version of a file has the
hash in the example above)


If you know the path of the file, you can start displaying all the commits for said path:

git log --all --pretty=format:%H <path>

If 0056c73 is a blob SHA1 for that file, a git ls-tree will print all entries SHA1, and you can grep the one you are after.

"Which commit has this blob?" proposes this one-liner from aragaer:

git log --all --pretty=format:%H <path> | xargs -n1 -I% sh -c "git ls-tree % <path> | grep -q <hash> && echo %"

(replace <hash> with 0056c73)