git:我如何找到两个分支的共同祖先......已经合并了 [英] git: how do I find the common ancestor of two branches... that have already been merged

查看:184
本文介绍了git:我如何找到两个分支的共同祖先......已经合并了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了找到2个git分支的共同祖先,需要做一件事:

  git merge-base branch another_branch 

好的。但是......如果两个分支已经合并了会怎么样?当我在这种情况下使用 merge-base 时,我得到的提交是合并前的最新提交,我想知道两个分支出来的共同祖先

换句话说:

  Z -  A  -  B  -  C  -  D  -  E  -  F 
\ /
G - H - I

HEAD 位于F.如果我在这里执行 git merge-base ,commit我得到的是 I ,我正在寻找的是A或者Z.是否有任何git指令可以找到它?
<如果你这样做
$ b $ pre $ g $ merge $ git merge-base CI $ b

$ b

你得到你想要的提交A.什么是C和我?它们是你的合并提交D的父提交。



Hoy你有他们吗?

  git log --pretty =%P -n 1 D 

会给你两个字符串。这两个字符串是您需要的提交C和我。



如果您想一起完成,您可以编写

  git merge-base $(git log --pretty =%P -n 1 MERGE_COMMIT)

您必须将MERGE_COMMIT替换为合并提交的散列D



编辑:
由于@poke声明使用

  git merge-base MERGE_COMMIT ^ MERGE_COMMIT ^ 2 

In order to find the common ancestor of 2 git branches, one needs to do:

git merge-base branch another_branch

Okay. But... what if both branches have already been merged? When I use merge-base in that case, the commit I get is the latest one before the merge, and I would like to know the common ancestor from which both branches came out "before the merge".

In other words:

Z - A - B - C - D - E - F
     \          /
      G - H - I 

HEAD is at F. If I do a git merge-base here, the commit I get is I, and the one I am looking for is A, or perhaps Z. Is there any git instruction to find it?

解决方案

If you do

git merge-base C I

you get the commit A you want. What are C and I? They are the parent commits of your merge commit D.

Hoy do you get them?

git log --pretty=%P -n 1 D 

gives you two strings. This two strings are the commit C and I you need.

If you want to do it all together you can write

git merge-base $(git log --pretty=%P -n 1 MERGE_COMMIT)

where you have to replace MERGE_COMMIT with the hash of your merge commit D

Edit: As @poke stated it is simpler with

git merge-base MERGE_COMMIT^ MERGE_COMMIT^2 

这篇关于git:我如何找到两个分支的共同祖先......已经合并了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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