Git:找到两个分支的最近共同祖先

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1549146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:11:25  来源:igfitidea点击:

Git: Find the most recent common ancestor of two branches

git

提问by Ted

How to find the most recent common ancestor of two Git branches?

如何找到两个 Git 分支的最近共同祖先?

回答by CB Bailey

You are looking for git merge-base. Usage:

您正在寻找git merge-base. 用法:

$ git merge-base branch2 branch3
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

回答by Acumenus

As noted in a prior answer, git merge-baseworks:

正如之前的回答中所指出的,git merge-base有效:

$ git merge-base myfeature develop
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

but if myfeatureis the current branch, as is common, you can use use --fork-point:

但如果myfeature是当前分支,这是常见的,您可以使用--fork-point

$ git merge-base --fork-point develop
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

This argument works only in sufficiently recent versions of git. Unfortunately it doesn't always work, however, and it is not clear why. Please refer to the limitations noted toward the end of this answer.

这个论点只适用于足够新的 git 版本。不幸的是,它并不总是有效,然而,也不清楚为什么。请参阅本答案末尾指出的限制。



For full commit info, consider:

有关完整的提交信息,请考虑:

$ git log -1 $(git merge-base --fork-point develop) 

回答by Martin G

With gitkyou can view the two branches graphically:

随着gitk您可以图形方式查看两个分支:

gitk branch1 branch2

And then it's easy to find the common ancestor in the history of the two branches.

然后很容易在两个分支的历史中找到共同的祖先。