Chinese Yellow Pages | Classifieds | Knowledge | Tax | IME

Problem:

For example: I have a merge in main dev branch, I want to cherry-pick that merge into a relatively old prod branch,

If we directly do: git cherry-pick merge_commit_in_dev_branch, we will get a  error:  is a merge but no -m option was given.

Solution

So we need to do this:

git cherry-pick -m 1  merge_commit_in_dev_branch

 

Why:

So what exactly -m 1 mean?

According to the document:  https://git-scm.com/docs/git-cherry-pick

-m parent-number--mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.
This option specifies the parent number (starting from 1) of the mainline and
allows cherry-pick to replay the change relative to the specified parent.

This doc tells us the reason, but may not easy to understand if you are not that familiar with git ( terms).

How to understand:

Let's look at an example to have a better idea, for example in main dev Branch:

commit 77397a
 Merge: f85f967e fae1cf07   ( f85f967e: parent id 1, fae1cf07 parent id 2, notes it is a commit showed below that line)
 Merge pull request 2

commit fae1cf07   ( parent id 2)
 commit 994bce4ec
 commit 445bd44f7

commit f85f967eb63a    ( previous merge commit , it is the parent id 1 point to)
 Merge: e088c228 7e9e27e5
 Merge pull request 1

 

In prod branch, if we do:

git cherry-pick -m 1 77397a

It tells git to replay the change relative to (starting from) parent id 1, which is f85f967eb63a in this case ( it was the previous merge commit)

to the current feature/prod branch.

So essentially, we tell git to replay all the changes/commit between previous merge and this merge commit to this prod branch.

That is exactly what we want ( as we do not need any commits before the previous merge commit).