ImaginativeThinking.ca


A developers blog

How the heck do you cherry pick in Git?

By: Brad

Hi Brad, in my code base I’ve been off working in a branch creating a new feature. In my efforts I’ve made a few other improvements to enable my new feature to work properly. The new feature I’m working on is not ready but the other improvements are and I’d like to cherry-pick them over to my main line. How do I do that?

git-pig

Good question; assuming your using Git and your other improvements were committed atomically, that is you committed one thing at a time instead of lumping them all together in an uber commit then Git has a really simply command for you.

Its called cherry-pick, for a tool created by the same person who created Linux this command is surprisingly named rather intuitively as it does exactly what you’d think. It cherry picks commits from one or more branches to another.

So in your case lets say you have one feature branch called feature and your main line called master.

Lets say that the improvements you want to cherry pick are in commits 964a79b77f6979c8f3dc551d2aedf0c7a018ac43, 01b068c7f0f1c22ca27dec6c61e14a652499f8db, and 5ded8b0670f479c1aad7bdaaae1400a8089fa1c1. Lets represent these commits in the below image as C3, C7, and C9.

git-cherry-pick-1

To get them from your feature branch to the master without also pulling in all the other new feature commits you can run the following commands:

    $ git checkout master
    $ git cherry-pick 964a7 01b06 5ded8

Where 964a7 01b06 5ded8 is the short from of the three commits we want to cherry pick.

The above will take the three commits from the feature branch and add them to your master branch under the current HEAD position.
Since commit sha’s are globally unique regardless of what branch they are in this would also work if the three commits we wanted to cherry pick each existed in different branches.

git-cherry-pick-2

There is a really good tutorial which I rather enjoyed going through located at http://pcottle.github.io/learnGitBranching
It goes through a number of commands, the cherry picking command is introduced at the Moving Work Around > Cherry-pick Intro level.

I hope that helped and answered your question.

Until next time think imaginatively and design creatively

Brad

My interest in computer programming started back in high school and Software Development has remained a hobby of mine ever since. I graduated as a Computer Engineering Technologist and have been working as a Software Developer for many years. I believe that software is crafted; understanding that how it is done is as important as getting it done. I enjoy the aesthetics in crafting elegant solutions to complex problems and revel in the knowledge that my code is maintainable and thus, will have longevity. I hold the designation Certified Technician (C.Tech.) with the Ontario Association of Computer Engineering Technicians and Technologists (OACETT), have been certified as a Professional Scrum Master level 1 (PSM I) and as a Professional Scrum Developer level 1 (PSD I) by Scrum.org as well as designated as an Officially Certified Qt Developer by the Qt Company. For more on my story check out the about page here

Feel free to write a reply or comment.