Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: determine revision based on a file

Tags:

git

revision

I have a file from a project that uses GIT as repository. For that file I need to find out to which revision this file belongs to. The file is stand-alone outside of an repository (not tracked) therefore the standard git commands do not work.

Is there a way to determine the revision this file belongs to only based on its filename and its content?

like image 474
Robert Avatar asked Sep 12 '11 12:09

Robert


People also ask

How do I revert to a previous version of a file in git?

In case you are using the Tower Git client, you can use the reset command right from a commit's contextual menu. And in case you made a mistake: simply hit CMD+Z to undo the reset and restore the removed commits!

How do you check all commits on a file?

Use git log --all <filename> to view the commits influencing <filename> in all branches.


1 Answers

I don't think there's a one-shot command to do this - git's object model makes it quite laborious to work back from a blob to commits that might reference it. Here's one way of doing it, though. First of all, find the hash of the file that git would use, with:

git hash-object foo.c

Suppose that returns f414f31. Then you can use a script like the following:

for c in $(git rev-list --all)
do
   ( git ls-tree -r $c | grep f414f31 ) && echo Found the blob in commit: $c
done

... to show all the commits that contain that blob. If you want to know which branches those commits are on, you can do:

git branch -a --contains 1a2b3c4d
like image 70
Mark Longair Avatar answered Oct 01 '22 11:10

Mark Longair