Cherry-picking your way out of trouble

I find cherry-pick to be one of those great underutilized features of git. And maybe that’s good, because it’s mainly used to apply hot fixes.

The way it works is very simple, it just let’s you merge one or more commits from one branch onto another. Awesome, right?

Imagine a situation in which you have two branches, master and payments-refactor.
You’re battling your way out of a thought refactor and suddenly a bug emerges in production, but you find out that you’ve already fixed it during the refactor and have an isolated commit containing the changes. You need to replicate those changes in the master branch and re deploy the app. But copy-pasting, or manually re doing the
changes, is cumbersome and probably even error prone. Well, cherry-pick comes to the rescue. It let’s us replicate that single commit onto the master branch, all while preventing duplicate work and keeping our git history clean.
The only thing we need is the abbreviated commit hash (or all of it), we move to the branch where we want to incorporate the changes and use it like this:

git checkout master
git cherry-pick 3f75a585

That’s all!

I hope you have to use this as little as possible, but find it useful when the time comes.

What do you think of cherry-pick, what do you use it for?

Leave a Reply

Your email address will not be published. Required fields are marked *