Git: move commits between branches with cherry-pick

Cherry-picking commits in Git is easy and straightforward. Here’s how to do that.

Basic usage of cherry-pick

Take two branches: new-feature and master, now we want to get one commit from new-feature to master:

$ git checkout new-feature

And see what commits have been issued in the past:

$ git log
 
commit 0b5e336cccf6b89894894b921d4858bb34417e02
Author: bob <[email protected]m>
Date:   Mon Apr 6 00:10:42 2015 +0200
 
Tweaks
 
commit cbc51480ea42dcc91deec8d8a30fbddc37d9b6d3
Author: bob <[email protected]m>
Date:   Sun Mar 1 12:37:11 2015 +0100
 
Change button color.

There seem to be two commits: "Tweaks" and "Change button color."

Maybe you need the "Tweaks" commit in the other branch, note down the commit hash. In this case it’s 0b5e336cccf6b89894894b921d4858bb34417e02.

Then check out the branch you want to put it in and cherry-pick it:

$ git checkout master
$ git cherry-pick 0b5e336cccf6b89894894b921d4858bb34417e02

Merge conflicts

It can be that the same file was edited in the other branch, then you might hit a merge conflict. If you do, first check the status of your repo:

$ git status

You’ll see all the files that are not added to the staging area, those are the ones with merge conflicts. Manually solve the conflicts, save the files, and then:

$ git add -A
$ git cherry-pick ---continue

That’s the exact same workflow as you would have when solving merge conflicts when rebasing. The longer version of that process is covered in the Git rebase and merge conflict workflow.