Cherry Picking Commits
In the standard Git workflow, we usually combine branches using Merge or Rebase. However, sometimes you don't want the whole branchโyou just want one specific "Win."
Cherry Picking allows you to pick a single commit from any branch in your repository and apply it to your current branch.
The term "Cherry Picking" is a bit whimsical, but it perfectly captures the idea of selecting just one "cherry" (commit) from a "bunch" (branch) without taking the whole thing.
The "Grocery Store" Analogyโ
Imagine you are at a grocery store.
- Merging is like buying the entire pre-packaged "Fruit Basket." You get the apples, the grapes, and the bananas, even if you only wanted the grapes.
- Cherry Picking is like walking up to the fruit stand, picking one perfect apple, and putting it in your own basket. You leave the rest of the fruit behind.
When to use Cherry Picking?โ
At CodeHarborHub, we use cherry-picking in these specific "Industrial" scenarios:
- Hotfixes: You fixed a critical bug in a
testingbranch, and you need that exact fix in themainbranch now, without pulling in all the other unfinished test code. - Undo a Revert: If you accidentally reverted a commit, you can cherry-pick the original commit back into your history.
- Collaborative "Theft": Your teammate wrote a perfect
Button.jscomponent on their messyexperimental-uibranch. You can cherry-pick just that component commit into your clean branch.
Step-by-Step: How to Cherry Pickโ
1. Find the Commit IDโ
First, you need the "ID" (the SHA hash) of the commit you want to steal. You can find this on GitHub or by using:
git log --oneline --all
Look for the 7-character code (like a1b2c3d) next to the commit message.
2. Switch to the Destinationโ
Move to the branch where you want the commit to land:
git checkout main
3. Pick the Cherryโ
Run the command followed by the commit ID:
git cherry-pick a1b2c3d
Merge vs. Cherry Pickโ
| Feature | Git Merge | Git Cherry Pick |
|---|---|---|
| Scope | Combines the entire branch history. | Copies one specific commit. |
| History | Creates a "Merge Commit." | Creates a brand new commit on your branch. |
| Use Case | Finishing a feature. | Porting a specific fix or component. |
Handling Cherry-Pick Conflictsโ
Just like a merge, if the "Cherry" you are picking changes the same lines as your current branch, a conflict will occur.
The Fix:
-
Open the file in VS Code.
-
Resolve the conflict as usual.
-
Run:
git add .
git cherry-pick --continue
When you cherry-pick, you are creating a copy of a commit. If you later merge the original branch into your current branch, Git is usually smart enough to know they are the same, but it can sometimes lead to a "messy" history. Use cherry-picking sparingly!