2012-11-19 57 views
2

我想查找另一个分支中包含的最近提交;鉴于历史在另一个分支中查找第一个祖先提交

A-B-C---E <-master 
    └-F---G <-develop 
    └-H-I <-branch1 

我想找到F.请注意,我只知道起点branch1,而不知道其他分支的名称。

我知道,我可以简单地检查最低i≥0使

git branch --contains branch1~i 

输出多行,但似乎浪费。

+0

我不确定我是否按照您要查找的内容。你想查找最早的(或最近的;我不知道“第一”是指“历史上的第一”还是“离我最近的地方”)提交包含在不同于当前分支的分支中?这是包含在当前的一个以及另一个分支?你的图实际上并没有显示任何分支(我假设E,G和我是提交),所以我不能告诉你计算的是哪一个。 –

+0

@BrianCampbell增加并澄清;该图确实太不精确。 – phihag

+0

那么,你想要的是'git merge-base branch1 develop',但是我不知道任何(简单)的方法来做到这一点,如果你不知道你的分支是从哪里产生的,合并基地“对每一个其他分支,然后以某种方式选择”赢家“... – twalberg

回答

3

为此,请查看Git分发附带的post-receive-email脚本(如果需要本地副本,应将其安装在某个地方,如/usr/share/doc/git-core/contrib/hooks/post-receive-email)。这有很长的评论,描述了如何只发现承诺是在给定的分支新的,和以前没有任何人看到:

# Consider this: 
    # 1 --- 2 --- O --- X --- 3 --- 4 --- N 
    # 
    # O is $oldrev for $refname 
    # N is $newrev for $refname 
    # X is a revision pointed to by some other ref, for which we may 
    # assume that an email has already been generated. 
    # In this case we want to issue an email containing only revisions 
    # 3, 4, and N. Given (almost) by 
    # 
    # git rev-list N ^O --not --all 
    # 
    # The reason for the "almost", is that the "--not --all" will take 
    # precedence over the "N", and effectively will translate to 
    # 
    # git rev-list N ^O ^X ^N 
    # 
    # So, we need to build up the list more carefully. git rev-parse 
    # will generate a list of revs that may be fed into git rev-list. 
    # We can get it to make the "--not --all" part and then filter out 
    # the "^N" with: 
    # 
    # git rev-parse --not --all | grep -v N 
    # 
    # Then, using the --stdin switch to git rev-list we have effectively 
    # manufactured 
    # 
    # git rev-list N ^O ^X 

有更多的细节处理在评论角落的情况下,和脚本的其余部分;但是,如果这个基本情况是所有你关心,这应该给你答案:

git rev-parse --not --all | grep -v I | git rev-list --stdin I 

在那里你可以计算I$(git rev-parse branch1)。结果的最后一项将是提交H,然后您可以使用H^分支到另一个分支到达最近的祖先。

+0

@phihag感谢您的编辑! –

+0

谢谢!这解决了它,稍作修改。 – phihag

0

如果你知道使用H-I分支的名称,例如BRANCH1

git reflog show --all|grep branch1

在荣誉最高的号码是您第一次提交该分支。

+0

嗯,这绝对有效,但感觉甚至比迭代* i *更有效率。首先,不应该需要reflog来回答这个问题。另外,'I'提交可能实际上不在任何分支中,或者处于多个分支中。而且应该真的grep for'refs/heads/branch1 @',以免'my_branch11'匹配。 – phihag

相关问题