You have to supply -m if the commit is a merge commit, i.e. a commit with more than one parent.
Normally, what git cherry-pick REV does can be described as:
- Take the changes between rev and its parent.
- Apply these changes to the current HEAD and commit the result with rev's commit message.
A merge commit joins two lines of development. For example, one line implements widget, and the other line removes clutter. The merge gives you the code with the widget, sans the clutter.
Now consider step #1 of the cherry-pick process: git can't guess whether you want to remove the clutter or to implement the widget. Nor can you do both, because the information on how to do both is not contained inside a single merge commit, only the content of the resultant merged tree is.
The -m option allows you to tell git how to proceed. For example, if clutter removal happened on master and the merge commit was created using git merge WIDGET, then git cherry-pick -m 1 merged-commit will cherry-pick the new widget because diff between the merged tree and parent 1 (the last of clutter-removing commits) will have been exactly the widget addition. On the other hand, git cherry-pick -m 2 merge-commit will delete the clutter, because the difference between parent 2 (the last of the widget-adding commits) and merge-commit is exactly the clutter-removal missing from the widget branch.
使用git show 可以查看这个commit的parent情况。如下面的例子,第三行“Merge: id1 id2”,就是这个commit的parent1,parent2
# git show <commit sha>
commit 44cf5105b99f2c18a0f9c24a2dab85ba40324949
Merge: 7d17f8f1 873cdba5
Author: wy <wy@xxxx.com>
Date: Thu Aug 11 10:31:24 2022 +0800
your commit log
其实,常用的git log也会显示commit的parent