GIT:根据文件确定修订版 [英] GIT: determine revision based on a file

查看:113
本文介绍了GIT:根据文件确定修订版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用GIT作为存储库的项目文件。对于那个文件,我需要找出这个文件属于哪个版本。该文件独立于版本库之外(未跟踪),因此标准git命令不起作用。



有没有一种方法可以根据文件的文件名和内容来确定这个文件所属的版本?

解决方案

我不认为有这样的一次性命令 - git的对象模型使得从blob回溯到可能引用它的提交非常费力。不过,这里有一种方法。首先,找到git将使用的文件的哈希值:

  git hash-object foo.c 

假设返回 f414f31 。然后你可以使用如下的脚本:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $($ git rev-list --all)

(git ls-tree -r $ c | grep f414f31)&& echo发现在提交blob:$ c
完成

...显示所有提交包含该blob。如果你想知道那些提交的分支,你可以这样做:

  git branch -a --contains 1a2b3c4d 


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?

解决方案

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

这篇关于GIT:根据文件确定修订版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆