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

Git 在另一个分支中查找第一个祖先提交,git,Git,我想找到包含在另一个分支中的最新提交;鉴于历史 A-B-C---E <-master └-F---G <-develop └-H-I <-branch1 输出多行,但这似乎很浪费。如果您知道带有H-I的分支的名称,例如branch1 git reflog show--all | grep branch1 获得最多荣誉的是您在该分支上的第一次提交。要做到这一点,请查看Git发行版附带的脚本(如果您想要本地副本,应该安装在/usr/share/doc/Git c

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

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

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

如果您知道带有H-I的分支的名称,例如branch1

git reflog show--all | grep branch1


获得最多荣誉的是您在该分支上的第一次提交。

要做到这一点,请查看Git发行版附带的脚本(如果您想要本地副本,应该安装在
/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^
到达另一个分支中的最新祖先。

好吧,这肯定有效,但感觉比迭代i更粗糙。首先,不需要reflog来回答这个问题。此外,
I
commit实际上可能不在任何分支中,也可能不在多个分支中。而且你应该非常感谢
refs/heads/branch1@
以免
我的分支11
也匹配。我不确定我是否符合你的要求。您想查找包含在不同于当前分支的分支中的最早(或最新;我不知道“first”是指“历史上的第一个”还是“离我所在位置最近”)提交?包含在当前分支和另一个分支中的?您的图表实际上没有显示任何分支(我假设E、G和I是提交),因此我无法分辨您正在计算哪些分支。@BrianCampbell补充并澄清;这个图确实太不精确了。好吧,你想要的是
git merge base branch1 develope
,但是如果你不知道你的分支是从哪里产生的,我不知道有什么(简单的)方法可以做到这一点,而不需要对其他分支进行
合并base
,然后以某种方式选择“赢家”…@phihag我想我还是想知道你为什么要这么做。你的最终目标是什么?另外,在您的原始图表中,您在现在标记为
branch1
的位置上有
HEAD
,但您说您正在寻找
F
H
;我不清楚
H
如何被视为在另一个分支上,因为它只在
branch1
上。如果你描述一下你的最终目标,可能会帮助我理解。我看到你又编辑了一遍,但你仍然说“其他分支”,而没有指定与哪个分支相对应的“其他”。你的意思是与branch1相比?如中所示,您正在尝试查找最近的提交,其中
branch1
不是该提交的唯一引用?
git rev-parse --not --all | grep -v I | git rev-list --stdin I