25

when I do git cherry-pick, I got this error, how can I solve it?

$ git cherry-pick  XXXXXXXXXXXXX                                                                                                                                          
error: commit XXXXXXXXXXXXX is a merge but no -m option was given.
fatal: cherry-pick failed
$ git cherry-pick
    -m, --mainline <n>    parent number
$ git cherry-pick -m 1234 XXXXXXXXXXXXX
$ error: commit XXXXXXXXXXXXX does not have parent 1234
fatal: cherry-pick failed
7
  • What happens if you do add a message?
    – wogsland
    Jan 25, 2017 at 1:54
  • And if you give a proposed option?
    – 0andriy
    Jan 25, 2017 at 1:55
  • I have updated post
    – Sato
    Jan 25, 2017 at 2:05
  • 1
    The parent numbers of a merge start at 1 and count up to the highest number of parents. In other words, parent #1 is the first parent, parent #2 is the second parent, parent #3 is the third parent of what must therefore be an octopus merge, and so on. What did you expect to get from parent #1234?
    – torek
    Jan 25, 2017 at 2:17
  • 1
    stackoverflow.com/a/41095200/1615903
    – 1615903
    Jan 25, 2017 at 6:12

2 Answers 2

33

I've had this same error when using git revert to revert a merge (feature branch which turned out to be bad). The -m is a bit confusing. Its not looking for a message. I think it just wants to know how far back from the given commit you want to revert (how many commits to revert)

Most of the time it's just 1. I.e. you only want to go back to the commit before your merge (the commit hash I'm providing). So the solution is:

git revert -m 1 <git_hash_for_merge>
2
  • 3
    the number of commits you want to step back from the commit you've referenced. It means I want to go back to the 1st commit before the commit hash I provided Nov 6, 2020 at 14:15
  • @robwilkins: Based on stackoverflow.com/questions/41095143 I think you are right that the value for -m specifies the number of commits to go back, but importantly it is based on the items as they appear in the log. Maybe that's obvious to some, but not to me. Further it seems important to understand that it specifies a line of the history to follow. I'm guessing that only certain values make sense. I think 1 always specifies a line, but other lines might not be at 2 or other values, right? This stuff is bit over my head, but these seem to be important details.
    – steve
    Apr 19, 2022 at 17:28
3

I think the help/man page explains pretty clearly why you need the -m parameter and the error message pretty clearly specifies that you need it:

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

4
  • 45
    I wouldn't say "pretty clearly", what does "cherry-pick" means, what does "side of the merge" means, what does "mainline" means, what is "parent number" what does "replaying changes" means. That quite a lot of rather unclear slang used here. Unless you are quite used to git I doubt you would know these terms.
    – cglacet
    Aug 6, 2020 at 15:26
  • 9
    Very unintuitive like most git commands. You merge something, you want to undo that, but you can't. First you have to read non-sensical documentation and since it doesn't help at all, you end up copying and pasting some random command and crossing fingers that it works.
    – laurent
    Sep 3, 2021 at 14:06
  • 3
    How do I find the mainline parent number? Sep 18, 2022 at 12:43
  • to add to how not-clear this help is, when it says you do not know which side of the merge should be considered the mainline, I would actually expect it to be much simpler: If I merge branch A onto master, and then I want to revert the merge commit while on master, it should be obvious that I want to go back to the state of master before I did the merge. I really don't understand how git became the winner of versioning systems... ¯_(ツ)_/¯
    – Rafa
    Dec 8, 2022 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.