24

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
Sato
  • 8,192
  • 17
  • 60
  • 115
  • What happens if you do add a message? – wogsland Jan 25 '17 at 01:54
  • And if you give a proposed option? – 0andriy Jan 25 '17 at 01:55
  • I have updated post – Sato Jan 25 '17 at 02: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 '17 at 02:17
  • 1
    http://stackoverflow.com/a/41095200/1615903 – 1615903 Jan 25 '17 at 06:12
  • Possible duplicate of [git cherry-pick says "...38c74d is a merge but no -m option was given"](https://stackoverflow.com/questions/9229301/git-cherry-pick-says-38c74d-is-a-merge-but-no-m-option-was-given) – beat Aug 29 '17 at 08:09
  • Does this answer your question? [git revert not allowed due to a merge but no -m option was given](https://stackoverflow.com/questions/24301390/git-revert-hash-not-allowed-due-to-a-merge-but-no-m-option-was-given) – Josh Correia Jun 17 '21 at 20:06

2 Answers2

32

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>
robjwilkins
  • 5,462
  • 5
  • 43
  • 59
  • 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 – robjwilkins Nov 06 '20 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 '22 at 17:28
2

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.

jbu
  • 15,831
  • 29
  • 82
  • 105
  • 44
    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 06 '20 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 03 '21 at 14:06
  • 3
    How do I find the mainline parent number? – retro_coder Sep 18 '22 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 08 '22 at 11:12