TIL-6 essential git cherry picking

Rahul Barwal
3 min readNov 7, 2023

--

Cherry picking is one of the essential git commands that one should invest their time learning. Recently my colleague and I were working on a feature branch.

We had our separate responsibilities, life was going good. During development we noticed an area which could be extracted out to a utility. When we discussed this in our standup, my “enthusiastic colleague 😛” had already done this and good thing about this was that he did it in an atomic commit. I was very happy and asked him to cherry-pick this commit of his to my branch and push it. He did not know about it. So I taught him and thought of this to be a good topic to write about.

I won’t tell you what the command is and what the options are. But I do want to tell you what are good use cases for this command and what magic does it do under the hood. Coming back to our development this is what it looked like:

Basically I wanted my colleague to pick this blue commit and push to my branch.

Wait… is this possible?

Yes the thing to understand about git is that it is just a series of commits that make sense when they come one after another.

Commit: a bundle of changes to files along with some other meta information.

So, technically it should not matter to git whether you did those commits yourself or not. And that’s how it is, and that’s what cherry pick allows. With this command you can pull changes to your branch. The only condition is that your HEAD has to be clean enough🙂(both literally and in git)

Get the commitID to be cherry picked

This can be done in multiple ways. And easiest of them is

git log
# (optional)
git log -oneline # simpler output

Let’s cherry-pick

Now that we have our commitID to be picked

git cherrypick CommitId1, CommitId2
# (optional) When recording the commit, append a line that says "(cherry picked from commit ...)"
git cherrypick -x COMMITID

Note: We can have multiple CommitIDs here.

Handling merge issues

Cherry-pick is not always easy and sometimes you land up with merge conflicts. In such scenarios git provides you with sequencer commands
1. — continue : once you fix and merge the conflict run git cherry-pick --continue to pick next commit ids in line
2. — skip: to skip the current commit and pick next commit in sequence
3. — quit: get rid of everything done till now.
4. — abort: Cancel the operation and return to earlier state.

Other use cases

Accidental commit to wrong branch

Sometimes when we have a quickfix in front of us we mistakenly commit it to wrong branch. Instead of repeating the same work in correct branch, we can simply cherry pick those commits.

Cherry-picking in production

One of the ways we take things to production are using release branches. Let’s assume you take a cut to take to production. Now your development branch goes on normally. You realise that there is a critical fix that will solve a major use case of prod users. Thera are two ways of handling this:

  1. Create a hot fix branch on top of your current release branch.
  2. Or simply cherry pick the commitID that introduces the fix and take this release branch to prod.

--

--