Cherry pick 显示没有给出 -m 选项错误

Cherry pick shows no -m option was given error

我有两个分支 master 和 development

我需要从 master 分支的开发分支获取一些提交 ID,所以我通过 cherry-pick 来完成,但它显示了一些错误

$> git cherry-pick cf0d52b

error: Commit cf0d52b900f990300c3aa17936ddbae1476d461a is a merge but no -m option was given.
fatal: cherry-pick failed

我没有收到这个错误,为什么会出现这个错误,我将如何摆脱它。

正如错误提示的那样。您尝试选择一个合并提交,因此您已经告诉 git 它应该针对哪个父级生成差异,然后将其应用于目标。

您正在尝试 cherry-pick 合并。合并是从几个 parent 建立起来的。您必须为合并提供 parent id。

您必须提供以下任何一项:

-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.


如何找出提交的 parent 是什么?

使用git show命令查看commit parents然后可以选择parent索引或者SHA-1

This option specifies the parent number (starting from 1)

直到 Git 2.13(2017 年第 2 季度),该数字可能为 0! (这是不正确的)

参见 commit b16a991 (15 Mar 2017) by Jeff King (peff)
(由 Junio C Hamano -- gitster -- in commit 9c96637 合并,2017 年 3 月 17 日)

cherry-pick: detect bogus arguments to --mainline

The cherry-pick and revert commands use OPT_INTEGER() to parse --mainline. The stock parser is smart enough to reject non-numeric nonsense, but it doesn't know that parent counting starts at 1.

Worse, the value "0" is indistinguishable from the unset case, so a user who assumes the counting is 0-based will get a confusing message:

$ git cherry-pick -m 0 $merge
  error: commit ... is a merge but no -m option was given.

The correct diagnosis is that "-m 0" does not refer to the first parent ("-m 1" does).

Try with git log --no-merges 将用于仅显示没有“合并散列”的提交。对于具有大量更改或合并的项目,这可能很有用