10

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?

Eric O Lebigot
  • 86,421
  • 44
  • 210
  • 254
Robert
  • 36,354
  • 15
  • 89
  • 140
  • 1
    i don't understand your question. Is your file tracked or not tracked? – TheOneTeam Sep 12 '11 at 12:26
  • 1
    Outside of an repository means "not tracked". – Robert Sep 12 '11 at 12:55
  • 1
    @Robert "Outside of the repository", to me, means that it was never tracked which would mean there is no version of it, therefor invalidating your question. I think the wording you're looking for is that the file has been tracked at some point in time. – Andy Sep 12 '11 at 13:03

1 Answers1

17

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
Mark Longair
  • 415,589
  • 70
  • 403
  • 320
  • A more robust check for the presence of the right hash would be `git ls-tree -r $c | cut -f 1 | cut -d' ' -f 3 | grep f414f31`: in principle, one of the file names could contain a hash number (though this is not be typical), and the two cut commands precisely select the git hashes (and leave out the file names). – Eric O Lebigot Dec 30 '21 at 14:40