简体   繁体   English

Git:如何列出合并分支上的提交?

[英]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分支上的提交——即XYZ Note that simply doing git log topic won't work -- that will return commits A and B as well.请注意,简单地执行git log topic行不通的——这也将返回提交AB

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 ,因此共享历史的topicmaster之间的第一个提交是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返回所有从mastertopic合并提交的 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来获取最后一个合并提交,即mastertopic的合并提交。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM