Cherry-picking a GIT merge commit from a branch.

Git doesn’t let you cherry pick a merge commit from one branch on to another branch. For e.g. a:

git cherry-pick ac61858fac21c33f47e95f3bac4c8cc3a643195b

Where ac61858 is a merge commit generates the following error:

error: Commit ac61858fac21c33f47e95f3bac4c8cc3a643195b is a merge but no -m option was given.

This is because the merge commit is merge of two lines of development. So when using it to cherry pick to another branch, GIT doesn’t know which of the two branches to merge to the destination branch.

This is explained @ http://stackoverflow.com/a/12628579/209952.

In order to get this to work, you need to use the -m option, with the parent to pick.

Also, cherry pick commits the changes with a message right away. If you want to avoid an immediate checkin, use the -n option.

So, say you have a RELEASE branch with two merge commits C1, C2. You want to merge these changes (and only these) back to, say, MASTER. Here’s how you would go about it:

git checkout MASTER
git cherry-pick -n -m 1 C1
git cherry-pick -n -m 1 C2
git commit -m"Merging feature xyz changes from RELEASE to MASTER"

Note the “-m 1” in the above commands. 1 means in include changes merged to RELEASE, 2 would mean use the state of RELEASE at the time of merge.

Leave a comment