[英]Git: How to list commits on a merged branch?
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:
假设我的 git 提交历史如下所示:
A---B---C---D---E---F master
\ /
X---Y---Z topic
How do I list the commits on the topic
branch -- that is, X
, Y
, and Z
?如何列出
topic
分支上的提交——即X
、 Y
和Z
? Note that simply doing git log topic
won't work -- that will return commits A
and B
as well.请注意,简单地执行
git log topic
行不通的——这也将返回提交A
和B
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.我以为我可以运行
git merge-base topic master
来找到B
,然后执行git log B..Z
来获取这些提交。 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 merge-base topic master
返回Z
,而不是B
,回想起来很有意义——因为Z
已经合并到 master ,因此共享历史的topic
和master
之间的第一个提交是Z
。
You can use the following command:您可以使用以下命令:
git log topic --not $(git rev-list master ^topic --merges | tail -1)^
git rev-list master ^topic --merges
returns the SHA-1 keys for all merge commits from master
to topic
; git rev-list master ^topic --merges
返回所有从master
到topic
合并提交的 SHA-1 密钥; 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
.因为可能有多个合并提交,所以我使用
tail -1
来获取最后一个合并提交,即master
和topic
的合并提交。
Then we log all commits for topic
while omiting all commits for the first parent of the merge commit ( --not <merge-commit>^
).然后我们记录
topic
所有提交,同时省略合并提交的第一个父项的所有提交( --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'
Which then can be used like this git <alias-name> topic master
.然后可以像这样使用
git <alias-name> topic master
。
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 版本 (1.9.0) 中,如果省略
tail
也可以使用,但我认为只返回相关的 SHA-1 密钥会更清晰。
If you only merged topic
to master
once , and you know that you merged topic
into master
in merge commit E
, you can simply use the <rev>^-<n>
revision syntax :如果您只将
topic
合并到master
一次,并且您知道在合并提交E
中将topic
合并到master
,则可以简单地使用<rev>^-<n>
修订语法:
git log E^-
If you don't remember the hash of commit E
and want an easy way to find it, based on the name of your branch ( topic
), you can use one of the answers in the question "Find merge commit which include a specific commit" .如果您不记得提交
E
的哈希值并希望通过一种简单的方法根据您的分支名称 ( topic
) 找到它,您可以使用问题“查找包含特定提交的合并提交”中的答案之一” 。 I personnally like git when-merged
:我个人喜欢
git when-merged
:
git when-merged topic
Putting it all together:把它们放在一起:
git log $(git when-merged -c topic)^-
If you merged topic
several times to master
, then the syntax above will only list the commits that were merged in the most recent merge commit.如果您多次将
topic
合并到master
,那么上面的语法将仅列出在最近的合并提交中合并的提交。 In that case, you can use rocketraman's mergedtopiclg
alias在这种情况下,您可以使用 Rocketraman 的
mergedtopiclg
别名
git mergedtopiclg $(git when-merged -c topic)
For completeness this alias is configured as为了完整起见,这个别名被配置为
mergedtopiclg = !sh -c \"git lg $(git oldest-ancestor $1^2 ${2:-master})..$1^2\" -
# The oldest ancestor between an integration branch (by default, master) and a topic branch (by default HEAD)
# e.g. git oldest-ancestor master ai/topic
# See http://stackoverflow.com/a/4991675/430128
oldest-ancestor = !bash -c 'diff --old-line-format= --new-line-format= <(git rev-list --first-parent \"${1:-master}\") <(git rev-list --first-parent \"${2:-HEAD}\") | head -1' -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.