寻找一个git commit来自哪个分支 [英] Finding what branch a git commit came from

查看:112
本文介绍了寻找一个git commit来自哪个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以找出它的sha1提交来自哪个分支?



如果您可以告诉我如何使用Ruby Grit完成此操作,可以获得额外奖励。

解决方案

尽管Dav是正确的,信息并没有直接存储,但这并不意味着你永远无法找到答案。这里有一些你可以做的事情。



找到提交的分支



  git branch --contains< commit> 

这会告诉你所有在他们的历史中有特定提交的分支。显然,如果提交已经被合并,那么这将不太有用。



搜索引荐日志



如果您正在在其中进行提交的存储库中,可以搜索该提交的行的reflog。超过90天的Reflog被git-gc修剪掉,所以如果提交时间太老,你就找不到它。也就是说,你可以这样做:

  git reflog show --all | grep a871742 

找到提交a871742。输出应该是这样的:

  a871742 refs / heads / completion @ {0}:commit(amend):mpc-完成:总重写

指示提交是在分支完成上进行的。默认输出显示缩写提交散列,所以一定不要搜索完整的散列或者您不会找到任何内容。



git reflog show 实际上只是的别名,所以如果你想摆弄输出格式来为grep提供不同的东西,这是你的出发点!



如果你不是在提交的存储库中工作,那么你可以做的最好的在这种情况下,检查推荐日志并找到提交首次引入到回购的时间;如果运气好的话,你会找到它承诺的分支。这有点复杂,因为你不能同时走提交树和reflog。您需要解析reflog输出,检查每个散列是否包含所需的提交。



查找后续合并提交



这是依赖于工作流程的,但具有良好的工作流程,提交将在合并后的开发分支上进行。您可以这样做:

  git log --merges< commit> .. 

查看具有给定提交作为祖先的合并提交。 (如果提交只合并一次,第一个应该是你之后的合并;否则你必须检查一些,我想)。合并提交消息应该包含合并的分支名称。 p>

如果您希望能够这样做,您可能需要使用 - no-ff 选项到 git merge 来强制合并提交创建,即使在快进的情况下也是如此。 (不过,如果过度使用,可能会变得模糊。)VonC的对相关问题的回答有助于详细阐述此主题。


Is there any way to find out what branch a commit comes from given its sha1?

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 --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. 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 repo; 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 commit来自哪个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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