GIT: determine revision based on a file

不想你离开。 提交于 2019-12-29 07:34:15

问题


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 it's filename and it's content?


回答1:


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


来源:https://stackoverflow.com/questions/7387768/git-determine-revision-based-on-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!