1

I have two branches develop and master. Say master is behind develop by ~20 commits. I want to merge only one of the commits (07f5722) from develop into master. How can I do that?

How to merge a specific commit in git and How do I merge a specific commit from one branch into another in Git? both say to use git cherry-pick without much of an explanation as to how you should use it. I didn't really follow the examples from the man page either.

I tried doing the following:

git checkout -b merge-07f5722 develop
git cherry-pick 07f5722

But received this message about the cherry-pick being empty:

On branch merge-07f5722
You are currently cherry-picking commit 07f5722.

nothing to commit, working directory clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

If you wish to skip this commit, use:

    git reset

Then "git cherry-pick --continue" will resume cherry-picking
the remaining commits.

The answer to git cherry-pick not working indicates that this means the cherry-pick is a no-op with no actual changes. Okay, so how do I get the changes?

NOTE: My question is nearly identical to git cherry-pick not working. However, I want to know how to accomplish what I intend to do instead of why the command I tried specifically failed.


The sequence of commands I had to use was:

git checkout -b merge-07f5722 master
git cherry-pick 07f5722 # commit from develop
git checkout master
git merge merge-07f5722
Community
  • 1
  • 1
All Workers Are Essential
  • 15,826
  • 38
  • 96
  • 129

2 Answers2

4

I think that your mistake was that you created your merge-07f5722 branch off of develop. Then when you tried to do the cherry-pick your changes were already on that branch because you already had the commit. So it would result in an empty commit. Create your merge branch off of master rather than develop should result in a successful cherry-pick for you.

Schleis
  • 34,455
  • 6
  • 60
  • 79
0

Another solution is (assuming all your commits are relatively atomic) to just rebase develop and move the commits in question to the bottom of the stack. Then only merge the few you need. This will make the merges fast-forward merges.

Although using master and develop in your example implies these are publicly published branches and unless you are prepared to tell everyone who ever cloned them that your rebasing the cherry-pick answer by @schleis is correct.

For completeness I'll just add this for those who would like to rebase over cherry-pick:

$ git checkout develop
$ git rebase -i master

An editor will popup with the list of commits (20 or so in your example) move the line for the commit(s) in question to the top of the file and save/exit)

$ git checkout master
$ git merge 07f5722 # The last commit you wanted

This will look like:

A--B (master)
    \
     C--D--E (develop)

After git rebase -i master and moving D to the beginning:

A--B (master)
    \
     D'--C'--E' (develop)

After git merge D from master:

A--B--D' (master)
       \
        C'--E' (develop)
Sukima
  • 9,627
  • 3
  • 42
  • 58