问题
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
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.
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
.
回答1:
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
; 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
.
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'
Which then can be used like this 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.
来源:https://stackoverflow.com/questions/23520125/git-how-to-list-commits-on-a-merged-branch