615

I made some changes in my master branch and want to bring those upstream. When I cherry-pick the following commits. However, I get stuck on fd9f578 where git says:

$ git cherry-pick fd9f578
fatal: Commit fd9f57850f6b94b7906e5bbe51a0d75bf638c74d is a merge but no -m option was given.

What is git trying to tell me and is cherry-pick the right thing to be using here? The master branch does include changes to files which have been modified in the upstream branch, so I'm sure there will be some merge conflicts but those aren't too bad to straighten out. I know which changes are needed where.

These are the commits I want to bring upstream.

e7d4cff added some comments...
23e6d2a moved static strings...
44cc65a incorporated test ...
40b83d5 whoops delete whitspace...
24f8a50 implemented global.c...
43651c3 cleaned up ...
068b2fe cleaned up version.c ...
fd9f578 Merge branch 'master' of ssh://extgit/git/sessions_common
4172caa cleaned up comments in sessions.c ...
Elias Zamaria
  • 80,938
  • 29
  • 103
  • 136
wufoo
  • 10,915
  • 12
  • 49
  • 78

5 Answers5

710

The way a cherry-pick works is by taking the diff a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying it to your current branch.

So, if a commit has two or more parents, it also represents two or more diffs - which one should be applied?

You're trying to cherry pick fd9f578, which was a merge with two parents. So you need to tell the cherry-pick command which one against which the diff should be calculated, by using the -m option. For example, git cherry-pick -m 1 fd9f578 to use parent 1 as the base.

I can't say for sure for your particular situation, but using git merge instead of git cherry-pick is generally advisable. When you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit. You lose all their history, and glom together all their diffs. Your call.

Borealid
  • 86,367
  • 8
  • 101
  • 120
  • 4
    @wufoo You should probably also learn about `git rebase` - it's like a merge, but instead of integrating two branches it transplants one to sit atop the other. – Borealid Feb 10 '12 at 17:00
  • 131
    how do you know the parent number? – Anentropic Mar 21 '12 at 14:32
  • 73
    @Anentropic 1 is the "first parent", 2 is the "second parent", and so on. The order is the one in which they're listed in the commit (as viewed by `git show` and the like). – Borealid Mar 21 '12 at 15:20
  • Another tip: sometimes it's helpful to use "git merge --squash ..." if you want to lose the extra history and you want to incorporate changes from all parents, recursively. – Mr Fooz Sep 19 '12 at 20:33
  • Not very clear from the cherry-pick help files is that if you want the 1st parent you should type the 2nd parent in the cherry-pick command. To emphasize the answer: "it collapses all the changes made in **the parent you didn't specify** to -m into that one commit". – Lode Nov 21 '12 at 18:00
  • In my case I accidentally did git reset --hard HEAD^ on a merge commit and wanted to get it back. Cherry-pick obviously failed with the subject message since it's not the right tool here. So I just edited .git/refs/heads/master to point back to the reset merge commit's SHA. All is good again. – lkraav Jun 05 '13 at 15:09
  • 2
    @lkraav You could also have just done a `git reset --hard HEAD@{1}` to get back your missing commit. `git reset` isn't restricted to moving "backwards" in the history. `git checkout -b mybranch HEAD@{1}` would also work. – Borealid Jun 06 '13 at 04:06
  • Am I weird for feeling chills up the spine reading about HEAD@{1}? :) Thanks for letting me know. – lkraav Jun 06 '13 at 11:03
  • 1
    @wufoo: Git is much safer than running with scissors -- in general, you can't ever really lose anything once it has been committed (or even stashed). It may seem like history is removed/rewritten by certain commands, but you can always find it again with `gitk --reflog`. In my case, I was able to use `git reset (sha)` followed by a re-commit to setup a cherry-pick around the "merge" that this message was complaining about (which had been produced as the results of a `git commit --amend`). – Brent Bradburn Sep 07 '15 at 01:51
  • 18
    WARNING: `git merge` may have unintended consequences. That command will add all other (older) commits that exist on the parent branch. Usually people choose to cherry-pick because they don't want the other commits. Be sure you double-check that you are implementing only the changes you want! – Kay V Apr 19 '18 at 22:57
  • 1
    @Borealid well, it's not that helpful to know that parent 1 is the 1st parent... Are they given a number at random? Or is there some structure (probably is), like parent 1 is the parent that is being merged into (i.e. it's a branch in which git merge otherBranch was called) or something. – Dan M. Feb 07 '19 at 13:13
95

-m means the parent number.

From the git doc:

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.

For example, if your commit tree is like below:

- A - D - E - F -   master
   \     /
    B - C           branch one

then git cherry-pick E will produce the issue you faced.

git cherry-pick E -m 1 means using D-E, while git cherry-pick E -m 2 means using B-C-E.

Afaq Ahmed Khan
  • 1,407
  • 1
  • 18
  • 29
gavincook
  • 1,224
  • 4
  • 6
  • 1
    This answer https://stackoverflow.com/a/38669506 provides good explanations for the `-m` value – Jmini Jan 27 '21 at 15:17
48

Simplify. Cherry-pick the commits. Don't cherry-pick the merge.

Here's a rewrite of the accepted answer that ideally clarifies the advantages/risks of possible approaches:

You're trying to cherry pick fd9f578, which was a merge with two parents.

Instead of cherry-picking a merge, the simplest thing is to cherry pick the commit(s) you actually want from each branch in the merge.

Since you've already merged, it's likely all your desired commits are in your list. Cherry-pick them directly and you don't need to mess with the merge commit.

explanation

The way a cherry-pick works is by taking the diff that a changeset represents (the difference between the working tree at that point and the working tree of its parent), and applying the changeset to your current branch.

If a commit has two or more parents, as is the case with a merge, that commit also represents two or more diffs. The error occurs because of the uncertainty over which diff should apply.

alternatives

If you determine you need to include the merge vs cherry-picking the related commits, you have two options:

  1. (More complicated and obscure; also discards history) you can indicate which parent should apply.

    • Use the -m option to do so. For example, git cherry-pick -m 1 fd9f578 will use the first parent listed in the merge as the base.

    • Also consider that when you cherry-pick a merge commit, it collapses all the changes made in the parent you didn't specify to -m into that one commit. You lose all their history, and glom together all their diffs. Your call.

  2. (Simpler and more familiar; preserves history) you can use git merge instead of git cherry-pick.

    • As is usual with git merge, it will attempt to apply all commits that exist on the branch you are merging, and list them individually in your git log.
Kay V
  • 1,917
  • 12
  • 14
39

@Borealid's answer is correct, but suppose that you don't care about preserving the exact merging history of a branch and just want to cherry-pick a linearized version of it. Here's an easy and safe way to do that:

Starting state: you are on branch X, and you want to cherry-pick the commits Y..Z.

  1. git checkout -b tempZ Z
  2. git rebase Y
  3. git checkout -b newX X
  4. git cherry-pick Y..tempZ
  5. (optional) git branch -D tempZ

What this does is to create a branch tempZ based on Z, but with the history from Y onward linearized, and then cherry-pick that onto a copy of X called newX. (It's safer to do this on a new branch rather than to mutate X.) Of course there might be conflicts in step 4, which you'll have to resolve in the usual way (cherry-pick works very much like rebase in that respect). Finally it deletes the temporary tempZ branch.

If step 2 gives the message "Current branch tempZ is up to date", then Y..Z was already linear, so just ignore that message and proceed with steps 3 onward.

Then review newX and see whether that did what you wanted.

(Note: this is not the same as a simple git rebase X when on branch Z, because it doesn't depend in any way on the relationship between X and Y; there may be commits between the common ancestor and Y that you didn't want.)

Daira Hopwood
  • 1,909
  • 18
  • 13
  • 1
    `git rebase Y` says `Current branch tempZ is up to date` – Basilevs Oct 10 '16 at 08:32
  • I think that means that `Y..Z` was already linear. So you can ignore that message and proceed with steps 3 and 4. – Daira Hopwood Oct 16 '16 at 13:48
  • 1
    Interesting idea, I had to draw it on paper to fully appreciate what was going on =D – Chris Jul 03 '17 at 11:21
  • 2
    Brilliant. git cherry-pick for a whole range complained either that the -m option was missing or that it was provided. Your solution was golden. (One suggestion: delete tempZ branch after) – Otheus Sep 06 '18 at 18:20
  • 1
    this is amazing! i've been struggling with cherry pick and only this made sense. I had a bit of trouble with the letters picked ( some branches and commits are capital letters and some branches are lowercase ) – pcarvalho Oct 30 '19 at 16:06
2

Simplification of @Daira Hopwood method good for picking one single commit. Need no temporary branches.

In the case of the author:

  • Z is wanted commit (fd9f578)
  • Y is commit before it
  • X current working branch

then do:

git checkout Z   # move HEAD to wanted commit
git reset Y      # have Z as changes in working tree
git stash        # save Z in stash
git checkout X   # return to working branch
git stash pop    # apply Z to current branch
git commit -a    # do commit
ephemerr
  • 1,457
  • 13
  • 15
  • 3
    This of course loses the metadata associated with the original commit. I guess it's a matter of opinion whether it is simpler. I do use it sometimes when I *want* to lose the metadata and only keep the overall code changes. Note that it works even if Y is not an immediate parent of Z (in which case the changes will be squashed). – Daira Hopwood Jul 05 '18 at 12:39