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?
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.
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.
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