在另一个分支中查找第一个祖先提交

时间:2012-11-19 18:48:09

标签: git

我想找到另一个分支中包含的最新提交;鉴于历史

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

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

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

git branch --contains branch1~i

输出多行,但这似乎很浪费。

1 个答案:

答案 0 :(得分: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^到达另一个分支中的最新祖先。