Finding Commits merged by a merge commit github

Finding Commits merged by a merge commit github

How to figure out what commits were merged by a merge commit ?
The ‘git merge-base’ command is the key to doing this. The following procedure should work for the typical merge commits we have in our trees, where the merge has exactly two parents and where one parent (typically the first parent) corresponds to the head of the mainline at the time of the merge, and the other parent (typically the second parent) corresponds to the head of the topic branch that was merged into the mainline.

You can use ‘git merge-base’ to find the merge base (i.e. the most recent common ancestor of the two parents), and then the commits merged from the topic branch are given by the commits between the merge base and the parent corresponding to the topic branch.
Here is an example from the staging branch .

Let’s say I want to find the commits merged in Pull Request #100. The name of the topic branch merged in this Software Requirement(SR) was “any_name”, so I can search for that in the git history to find the merge commit:
$ git log –oneline –grep “any_name” origin/staging
asdae2c Merge remote-tracking branch ‘tarry/any_name’ into staging

Now find the parents of this commit:
$ git log -1 –pretty=format:%P asdae2c
4555dd4677cf24dfabeb24ae1740581fedb4bbfa a7f4b7cf795a7841cd4433115f8706bcbda6d33f

‘git merge-base’ will tell us the common ancestor of the parents:
$ git merge-base 4555dd4677cf24dfabeb24ae1740581fedb4bbfa aaaab7cf795a7841cd4433115f8706bcbda6d33f
23cc8c61c29aecc51d1b2e3a0448fa84df7e2bd2

Assuming the second parent corresponds to the topic branch, we can now use ‘git log’ to list all of the commits merged from the topic branch:
$ git log –oneline 23cc8c61c29aecc51d1b2e3a0448fa84df7e2bd2..aaaab7cf795a7841cd4433115f8706bcbda6d33f
b7f4b7c “commit Message1”
e032c7a “commit Message2”
4c8e5f5 “commit Message3”
6a83092 “commit Message4”
10d08e0 “commit Message5”

You can verify from the github pull request that those 5 commits are indeed the ones that were merged.
You can put all the commands together like this:
$ export MERGE_COMMIT=asdae2c
$ git log –oneline $(git merge-base $(git log -1 –pretty=format:%P ${MERGE_COMMIT}))..$(git log -1 –pretty=format:%P ${MERGE_COMMIT} | cut -f 2 -d’ ‘)
b7f4b7c “commit Message1”
e032c7a “commit Message2”
4c8e5f5 “commit Message3”
6a83092 “commit Message4”
10d08e0 “commit Message5”

Leave a Reply

Your email address will not be published. Required fields are marked *