It’s no secret that Git is an essential tool for all kinds of software. Git isn’t terribly hard to pick up, yet has a complex learning curve. The neat thing about the learning curve is the little tricks just waiting to be picked up. Today, I will be going over something that can be massively useful in select scenarios.
Why Use Cherry Pick?
Have you ever wanted to “pick” a single commit from one branch to another? That’s exactly what cherry-pick
is for. There is a difference between merging and picking, and you should not use it as a replacement for a merge/rebase. While it’s not an alternative solution to a merge, it certainly has its use cases.
Use Case 1: You accidentally committed to the wrong branch.
We all make mistakes, even if our tools are designed to prevent them. Let’s say you have a master
branch and a feature
branch. You meant to make a commit for a new feature, but accidentally committed your progress to master. Uh oh! Luckily, this problem isn’t too big of a deal.
There are several ways to fix this, but git cherry-pick
is a fairly straightforward and practical solution.
Use Case 2: You want to quickly move a specific feature/hotfix over to a production branch.
In short: you can move a certain commit without merging an entire branch. Why is this useful? Bugs happen. They can happen on old commits, or as a sneaky line of code that made it through the last review. The fastest way to apply a hotfix is to move it from a different branch without merging it.
It’s usually not practical to merge an entire branch before it’s ready, especially just to fix something small.
Git cherry-pick
is useful for applying a hotfix quickly without merging a branch into master.
Use Case 3: Picking useful features from a stale branch
Sometimes, features and implementations don’t get merged. That’s fine, it happens. Sometimes though, there are specific commits that might still be useful for a different branch. This is practical when you want certain code, but not the whole branch.
You’re effectively “picking” what you what, if you will.
How To Use Git Cherry Pick
Using cherry-pick
is straightforward. All you need is at least two branches. One where you want to apply the change, and the destination where you’re picking from. You will checkout the branch you want to apply to.
To select the commit you will pick, you will need the commit ID. You can easily get this by running git log
.
Once you have the ID, checkout the branch you want to pick the commit to. For example:
. I want to pick a commit to master, so I’d run git checkout master
.git cherry-pick <id>
For example, if I wanted to pick the “snowstorm implementation” commit to master, I’d
and run git checkout master
git cherry-pick 35349e685be7fd91c9cebeda1ea7ce2c4906a5b0
.
That’s pretty much it! If you want to learn more about what options you have or how this works, feel free to check out the list of resources below.
Additional Resources
https://www.atlassian.com/git/tutorials/cherry-pick – An overall good resource for learning about cherry-pick
https://git-scm.com/docs/git-cherry-pick – Official docs
https://stackoverflow.com/questions/9339429/what-does-cherry-picking-a-commit-with-git-mean – More in-depth explanation and further examples
Oh shit, git – Contains a practical use of git cherry-pick
Comments