查找 Git 提交来自哪个分支 [英] Finding what branch a Git commit came from
问题描述
有没有办法根据提交的 SHA-1来找出提交来自哪个分支> 哈希值?
如果您能告诉我如何使用 Ruby Grit 完成此任务,则可以获得奖励积分.
虽然 Dav 认为信息没有直接存储是正确的,但这并不意味着您永远无法找到.您可以执行以下操作.
查找提交所在的分支
git branch -a --contains <提交>
这将告诉您在其历史记录中具有给定提交的所有分支.显然,如果提交已经被合并,这就没那么有用了.
搜索引用
如果您在进行提交的存储库中工作,则可以在引用日志中搜索该提交的行.超过 90 天的 Reflog 会被 git-gc 修剪,所以如果提交太旧,你将找不到它.也就是说,您可以这样做:
git reflog show --all |grep a871742
找到提交 a871742.请注意,您必须使用提交的 abbreviatd 前 7 位数字.输出应该是这样的:
a871742 refs/heads/completion@{0}:提交(修改):mpc-completion:完全重写
表示提交是在分支完成"上进行的.默认输出显示缩写的提交哈希,因此请确保不要搜索完整的哈希,否则您将找不到任何内容.
git reflog show
实际上只是 git log -g --abbrev-commit --pretty=oneline
的别名,所以如果你想摆弄输出格式以使 grep 可以使用不同的内容,这就是您的起点!
如果您不在进行提交的存储库中工作,那么在这种情况下,您可以做的最好的事情是检查引用日志并找到提交首次引入存储库的时间;幸运的是,您获取了它承诺的分支.这有点复杂,因为您不能同时遍历提交树和引用日志.您想解析 reflog 输出,检查每个哈希以查看它是否包含所需的提交.
查找后续合并提交
这取决于工作流,但如果工作流良好,则在开发分支上进行提交,然后将其合并.您可以这样做:
git log --merges ..
查看将给定提交作为祖先的合并提交.(如果提交只合并一次,第一个应该是您要进行的合并;否则我想您必须检查一些.)合并提交消息应包含已合并的分支名称.>
如果您希望能够指望这样做,您可能需要使用 --no-ff
选项到 git merge
以强制合并提交创建甚至在快进的情况下.(不过不要太急切.如果过度使用,这可能会变得模糊.)VonC 对相关问题的回答 对这个主题进行了有益的阐述.
Is there a way to find out what branch a commit comes from given its SHA-1 hash value?
Bonus points if you can tell me how to accomplish this using Ruby Grit.
While Dav is correct that the information isn't directly stored, that doesn't mean you can't ever find out. Here are a few things you can do.
Find branches the commit is on
git branch -a --contains <commit>
This will tell you all branches which have the given commit in their history. Obviously this is less useful if the commit's already been merged.
Search the reflogs
If you are working in the repository in which the commit was made, you can search the reflogs for the line for that commit. Reflogs older than 90 days are pruned by git-gc, so if the commit's too old, you won't find it. That said, you can do this:
git reflog show --all | grep a871742
to find commit a871742. Note that you MUST use the abbreviatd 7 first digits of the commit. The output should be something like this:
a871742 refs/heads/completion@{0}: commit (amend): mpc-completion: total rewrite
indicating that the commit was made on the branch "completion". The default output shows abbreviated commit hashes, so be sure not to search for the full hash or you won't find anything.
git reflog show
is actually just an alias for git log -g --abbrev-commit --pretty=oneline
, so if you want to fiddle with the output format to make different things available to grep for, that's your starting point!
If you're not working in the repository where the commit was made, the best you can do in this case is examine the reflogs and find when the commit was first introduced to your repository; with any luck, you fetched the branch it was committed to. This is a bit more complex, because you can't walk both the commit tree and reflogs simultaneously. You'd want to parse the reflog output, examining each hash to see if it contains the desired commit or not.
Find a subsequent merge commit
This is workflow-dependent, but with good workflows, commits are made on development branches which are then merged in. You could do this:
git log --merges <commit>..
to see merge commits that have the given commit as an ancestor. (If the commit was only merged once, the first one should be the merge you're after; otherwise you'll have to examine a few, I suppose.) The merge commit message should contain the branch name that was merged.
If you want to be able to count on doing this, you may want to use the --no-ff
option to git merge
to force merge commit creation even in the fast-forward case. (Don't get too eager, though. That could become obfuscating if overused.) VonC's answer to a related question helpfully elaborates on this topic.
这篇关于查找 Git 提交来自哪个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!