Git:如何列出合并分支上的提交? [英] Git: How to list commits on a merged branch?

查看:245
本文介绍了Git:如何列出合并分支上的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何列出分支上的提交但不列出合并的分支,因此我将以相同的格式进行询问。假设我的git commit历史记录如下:

This is the inverse of how to list commits on a branch but not merged branches, so I'll ask it in the same format. Let's say my git commit history looks like this:

A---B---C---D---E---F master
     \         /
      X---Y---Z topic

如何在 topic 分支上列出提交-即 X Y Z ?请注意,仅执行 git日志主题将不起作用-这将返回提交 A B

How do I list the commits on the topic branch -- that is, X, Y, and Z? Note that simply doing git log topic won't work -- that will return commits A and B as well.

我以为我可以运行 git merge-base topic master 查找 B ,然后执行 git log B..Z 来获取这些提交。不幸的是, git merge-base topic master 返回了 Z ,而不是 B ,这回想起来很有意义-由于 Z 已被合并到master,因此 topic 和共享历史记录的 master Z

I had thought I could run git merge-base topic master to find B, and then do git log B..Z to get these commits. Unfortunately, git merge-base topic master returns Z, not B, which makes sense in retrospect -- since Z is already merged to master, the first commit between topic and master that shares history is Z.

推荐答案

您可以使用以下命令:

git log topic --not $(git rev-list master ^topic --merges | tail -1)^

git rev -list master ^ topic --merges 返回从 master topic 的所有合并提交的SHA-1键。 code>;因为可能有多个合并提交,所以我使用 tail -1 来获取最后一个合并提交,即 master topic

git rev-list master ^topic --merges returns the SHA-1 keys for all merge commits from master to topic; since it's possible that there are multiple merge commits I use tail -1 to just get the last merge commit which is the merge commit of master and topic.

然后,我们记录所有针对 topic 的提交,而忽略了合并提交的第一个父级的所有提交(-不是< merge-commit> ^ )。

Then we log all commits for topic while omiting all commits for the first parent of the merge commit (--not <merge-commit>^).

要有效使用此命令,我将定义如下别名:

To use this command effectively I would define an alias as follows:

git config --global alias.<alias-name> '!f() { git log $1 --not $(git rev-list $2 ^$1 --merges | tail -1)^; }; f'

然后可以这样使用 git< alias-name> ;主题大师

仅供参考:

在我的git version(1.9.0),如果您省略 tail 也可以,但是我认为只返回相关的SHA-1密钥会更清洁。

Just for your information:
In my git version (1.9.0) it would also work if you omit tail but I thought it would be cleaner to only return the relevant SHA-1 key.

这篇关于Git:如何列出合并分支上的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆