Skip to main content

Intro to Cherry Picking with Git

When you are working with a team of developers on a medium to large sized project, managing the changes between a number of git branches can become a complex task. Sometimes you don't want to merge a whole branch into another, and only need to pick one or two specific commits. This process is called 'cherry picking'.

by Pasan Gamage /

When should we use Cherry Picking?

I will try to explaining this through a scenario which will make it easier to understand.

Let’s say you are working in an project where you are making changes in a branch called new-features. You have already made a few commits but want to move just one of them into the master branch.

  • From new-features branch run git log --oneline  to get a better log of your commits history. Note that the commit hash is what we need to start the cherry picking. For example:

Git log output

  • Checkout the branch where you want to cherry pick the specific commits. In this case master branch:

git checkout master

  • Now we can cherry pick from new-features branch:

git cherry-pick d467740

This will cherry pick the commit with hash d467740 and add it as a new commit on the master branch. Note: it will have a new (and different) commit ID in the master branch.

If you want to cherry pick more than one commit in one go, you can add their commit IDs separated by a space:

git cherry-pick d467740 de906d4

  • If the cherry picking gets halted because of conflicts, resolve them and

git cherry-pick --continue

  • If you want to bail of this step out altogether, just type:

git cherry-pick --abort

After all this is done, you can simply push the new commits to the upstream repo (e.g origin) and get on with your day.

Tips n Tricks

In case you needed to cherry pick a merge instead of a commit, you can use:

git cherry-pick -m 1 <hash>

Caution!

Cherry picking is commonly discouraged in developer community. The main reason is because it creates a duplicate commit with the same changes and you lose the ability to track the history of the original commit. If you can merge, then you should use that instead of cherry picking. Use it with caution!

Further Reading

Here is some good material for broadening your knowledge:

  1. https://www.devroom.io/2010/06/10/cherry-picking-specific-commits-from-another-branch/

  2. http://think-like-a-git.net/sections/rebase-from-the-ground-up/cherry-picking-explained.html

  3. https://git-scm.com/docs/git-cherry-pick

Tagged

Git

Posted by Pasan Gamage
Drupal Developer

Dated

Comments

Comment by Lesly

Dated

Nice article, thank you!

Comment by Nyancat

Dated

This is a brilliant, short, to-the-point guide. Bookmarking it as my git-cherry-pick cheat sheet :)

Comment by Edson

Dated

Nice explanation!

I work in an application that is default for most clients but there are two clients that have it a little bit customized, so I use the same git repository but different branches to develop for those two separate clients.

When I want to add a feature from the default app to a customized one, I have two options:
Use merge or Pull Requests, it'll bring the past history along with it.
Cherry pick, bring only specific changes and ignore the past history.

I learned Cherry pick recently and it has helped me very much.
I use cherry pick to add a feature from the default app to a customized one, when I don't want to bring the past history of that feature.

Comment by Prashant Aggarwal

Dated

Seems like a legit approach to me!
Thanks for sharing a specific scenario.

Comment by Andy

Dated

Very nicely done, thank you for that!

Comment by Sanjay Bharatiya

Dated

Very well to-the-point explanation. Thanks.

Comment by Rubens

Dated

Very clear explanation. Thanks.

Comment by Pratik

Dated

Nice explanation...

Comment by Hari

Dated

Awesome handy note. Thanks For sharing!!

Comment by Bojan-018

Dated

Thank youuu for free sharing of knowledge! :)

Comment by Dharmik

Dated

this is cool article ! understood it all in one read. Thanks :D

Comment by H Thomas

Dated

Very simple and helpful. Thanks

Comment by Ashutosh Narayan

Dated

An article to the point explained and easily understandable. Thank you.

Comment by Jaiganesh

Dated

Simply superb and helpful.

Comment by Suhaib

Dated

Damn this tutorial was so smooth. GJ

Comment by Philip

Dated

Thank you for the great explanation. One question I have is, what happens when you try to merge the branch that you cherry picked from into the branch that you've cherry picked to? Using your example, what happens when the rest of your "new-features" branch is ready to be merged into master? Will the merge have conflicts from trying to merge in code that's already changed? Should I rebase my "new-features" branch and remove the commits that I cherry picked out?

Comment by Bob

Dated

That's awesome, thanks for sharing

Comment by John C

Dated

Short and sweet. Thanks for the help.

Comment by walls00000

Dated

Nice page! Thank you!

Comment by Aolphn

Dated

I think cherry-pick can use in following situation.

1.We commit a bug fix(assume this commit hash is 012345) on dev_br_2016_03(a branch is older than current branch);

2.We need this bug fix on branch dev_br_2019_03 ;

3.We can checkout dev_br_2019_03 and do git cherry-pick 012345 for current branch.

Comment by PJ.life

Dated

Great article! Thanks for the information 🤙

Comment by Liam Proven

Dated

Good, simple, clear explanation.

You seem to have lost an illustration somewhere, though. There's just an image that says:

«
Screen Shot 2017-09-08 at 12.56.24 pm.png
»

Comment by Faruk

Dated

It helps. Thanks for your article.

Comment by Mahran Elmasri

Dated

Good explain

Pagination

Add new comment

Restricted HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Not sure where to start? Try typing "hello" or "help" if you get stuck.