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

标签 git

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

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

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

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

git branch --contains branch1~i

输出多行,但看起来很浪费。

最佳答案

为此,请查看 post-receive-email你的 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^ 到达另一个分支中的最近祖先。

关于git - 在另一个分支中查找第一个祖先提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13460152/

相关文章:

GIT:持有 push 批准?

python - 只需从github下载.git目录

git - 在 git bundle 中提交

git - 自定义 merge 请求详细信息页面

git - 声明式 Jenkins 管道和 Docker

linux - 如何在 bash 中显示 git 输出并将输出中的一个字符串存储在变量中?

git - 如何在 git 标记消息中包含换行符

Git 全局标签--- 显然不好,但为什么呢?

git - 使 gh-pages 与 master 的最新提交保持同步

git:重命名文件并更改文件内容