Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Git commit given just a version of file

Tags:

git

If you only have a file (only contains text data), how can you determine which commit in history it maps to?

This is useful for finding a version of a project when someone has ripped a subset of the files out of the repository and dumped them somewhere.

like image 995
Matt Kneiser Avatar asked Dec 06 '25 18:12

Matt Kneiser


1 Answers

1) Iterate through all the objects in the repository (.git/objects/*), diff each file (using git cat-file) with your given file

2) When you have found the SHA of the object that matches the input file, find the commit containing that file's SHA. Iterate through all commits and use git ls-tree | grep to find which commit contains the object.

I consolidated this solution into a simple, easy-to-run script here: https://gist.github.com/themattman/20ec6da84304740972e057c22b15c0ee

like image 115
Matt Kneiser Avatar answered Dec 08 '25 12:12

Matt Kneiser