Cherry pick shows no -m option was given error
Asked Answered
A

5

49

I have two branch master and development

I need to get some commit id from development branch in master branch so I do it by cherry-pick but it shows me some error

$> git cherry-pick cf0d52b

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

I am not getting this error, why this error comes and how will I get rid of this.

Ambriz answered 2/5, 2016 at 6:12 Comment(1)
Possible duplicate of git cherry-pick says "...38c74d is a merge but no -m option was given"Kyle
K
53

You are trying to cherry-pick a merge. A merge is build up from at several parents. You have to supply the parent id to the merge.

You have to supply any of the followings:

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


How to find out what are the commit parents?

Use the git show command to view the commit parents and then you can choose the parent index or the SHA-1

enter image description here

Kalvn answered 2/5, 2016 at 7:12 Comment(5)
I still don't get how to find the parent numberDavidadavidde
In the screenshot above there are 2 ids, d684a65 and d23e8e8, how to know which one refers to the branch I am on?Davidadavidde
In addition to @RaphaelPinel's answer, you can check the git log and check which branch holds the commit you want to cherry-pick. That commit should have one of the ID:s displayed under "Merge: ". Typically the command will then be: git cherry-pick -m 1 <SHA>Undertake
In addition to @RaphaelPinel and @MikaelHalen, git log --no-merges would be used to display commits without "merge hash"; for projects with along numbers of changes or merges this could be usefulBookstore
@Raphael Pinel the first parent is the first hash (in this case, d684a65). This hash is the "parent" hash on which d23e808 was merged. Or, commit 5717a50 is the "merged" result of merging d23e808 (parent 2) into d684a65 (parent 1.)Idioblast
S
12

This option specifies the parent number (starting from 1)

Up until Git 2.13 (Q2 2017), that number could be 0! (which is incorrect)

See commit b16a991 (15 Mar 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 9c96637, 17 Mar 2017)

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

Solent answered 10/4, 2017 at 21:10 Comment(1)
Yes, I'm using git version 2.21.0 and -m 1 works.Classmate
G
3

Just as the Error suggest. You try to pick a merge commit, so you have tell git against which parent it should produce the diff that is then applied to the target.

Galton answered 2/5, 2016 at 6:24 Comment(0)
B
2

Try with git log --no-merges would be used to only display commits without "merge hash". For projects with along numbers of changes or merges this could be useful

Bookstore answered 25/1, 2021 at 23:22 Comment(0)
A
0

In some cases, if you don't find the parent commit,

you can do below by picking commit before merge.

So, if you have a specific commit before any merge, you can do that.

git cherry-pick (commit-id)

Also, multiple commit IDs

git cherry-pick (commit-id) (commit-id-2) ..... (commit-n)

Also, you can do this by selecting parent commit id:

How to find it? -> Click on commit (View Commit Details) (github, bitbucket, gitlab, etc)

enter image description here

Athos answered 12/4 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.