Show commits involved in a prior git merge

2019-01-21 16:49发布

问题:

Say I create a hotfix branch off of a develop branch, make two commits, merge this back to the develop branch, and destroy the hotfix branch.

How do I find out what commits were part of the merge? Is that possible?

回答1:

If you want to see every commits merged in the last merge you can try that :

git log $(git merge-base --octopus $(git log -1 --merges --pretty=format:%P)).. --boundary

Here is an example of my current log :

$ git log --graph --pretty=oneline --abbrev-commit
* 44899b9 pouf
*   8f49f9c Merge branch 'test'
|\  
| * 3db39ca test
* | 69f431c pif
* | df1f51c lala
|/  
* 8fae178 pif2
* 20f8ba6 init

If I only want commits related to the last merge I have to use git log -1 --merges --pretty=format:%P which gives me the parents of the first merge available :

$ git log -1 --merges --pretty=format:%P
69f431cec7859b61d33c7503c9431ceea2aaf3e0 3db39ca3ab1e8f70462db23d94590628b5e7ad7b

Now that I know which parents I need to track, I need their common base that I can obtain through git merge-base --octopus (--octopus is there just in case) :

$ git merge-base --octopus $(git log -1 --merges --pretty=format:%P)
8fae178666e34a480b22e40f858efd9e7c66c3ca

Now with git log I can search every commit since the base to the current HEAD and voilà :

$ git log $(git merge-base --octopus $(git log -1 --merges --pretty=format:%P)).. --boundary --graph --pretty=oneline --abbrev-commit 
* 44899b9 pouf
*   8f49f9c Merge branch 'test'
|\  
| * 3db39ca test
* | 69f431c pif
* | df1f51c lala
|/  
o 8fae178 pif2

If you're a bit perfectionist you can also do this :

$ git log $(git merge-base --octopus $(git log -1 --merges --pretty=format:%P))..$(git log -1 --merges --pretty=format:%H) --boundary --graph --pretty=oneline --abbrev-commit 
*   8f49f9c Merge branch 'test'
|\  
| * 3db39ca test
* | 69f431c pif
* | df1f51c lala
|/  
o 8fae178 pif2

Now I think I'll keep this as an alias :)

PS: Obviously you don't have to keep the --graph --pretty=oneline --abbrev-commit options


Resources :

  • Git merge-base
  • Git log
  • Git revision


回答2:

Say your merge commit is ab2f8173, git log ab2f8173^..ab2f8173 will show the commits which it merged in.

Here is how to turn this into a git alias for easy re-use:

$ git config --global alias.merge-log '!f() { git log --stat "$1^..$1"; }; f'
$ git merge-log 0865c12


回答3:

If you have a merge commit (say a2345) and say git log -1 a2345, it will tell you the names of the parents (i.e. the commits which got merged in this commit). Is that what you're looking for?