clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Git: move commits between branches with cherry-pick

Filed under: Webdev— Tagged with: git

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 <bob@example.com>
Date:   Mon Apr 6 00:10:42 2015 +0200

Tweaks

commit cbc51480ea42dcc91deec8d8a30fbddc37d9b6d3
Author: bob <bob@example.com>
Date:   Sun Mar 1 12:37:11 2015 +0100

Change button color.

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

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

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!