2

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

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

我要找F。注意我只知道起点branch1,不知道其他分支的名字。

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

git branch --contains branch1~i

输出不止一行,但这似乎很浪费。

4

2 回答 2

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)。结果的最后一个条目将是 commit ,您可以使用then到达另一个分支H中最近的祖先。H^

于 2012-11-19T20:03:35.677 回答
0

如果您知道带有 HI 的分支的名称,例如 branch1

git reflog show --all|grep branch1

荣誉中的最高数字是您在该分支上的第一次提交。

于 2012-11-19T19:14:07.490 回答