The Hook (The "Byte-Sized" Intro)
A teammate accidentally committed a critical bugfix on the wrong branch. The whole branch is messy and can't be merged. But that one commit? It's perfect. git cherry-pick copies exactly that one commit onto your current branch — no merge, no mess, just the fix. It's a surgical strike for commit-level precision.
📖 What is Cherry-pick for Recovery?
git cherry-pick takes a specific commit by its SHA and replays it on your current branch, creating a new commit with the same changes but a new hash. It's used for targeted recovery, hotfixes, and selective backporting.
Conceptual Clarity
What cherry-pick does:
Before:
main: A ─ B ─ C
feature: D ─ E ─ F (you want only E)
After cherry-pick E onto main:
main: A ─ B ─ C ─ E'
feature: D ─ E ─ F (unchanged)
Common use cases:
| Scenario | How Cherry-pick Helps |
|---|---|
| Bugfix on wrong branch | Copy the fix commit to the right branch |
| Selective backport | Apply one feature commit to a release branch |
| Recovery after reset | Rescue a specific commit from reflog |
| Partial merge | Need 1 commit from a branch, not all of them |
Real-Life Analogy
Cherry-pick is like photocopying one page from a book and pasting it into a different notebook. The original book stays the same, and your notebook gets exactly the content you need.
Visual Architecture
Why It Matters
- Precision: Copy exactly one commit without merging an entire branch.
- Recovery: Rescue individual commits from deleted branches or reflog.
- Hotfixes: Apply a fix from develop to a release branch without a full merge.
- Safety: Non-destructive — the source branch is unchanged.
Code
# ─── Cherry-pick a single commit ───
git switch main
git cherry-pick abc1234
# Creates a new commit E' with the same changes as abc1234
# ─── Cherry-pick without auto-committing ───
git cherry-pick abc1234 --no-commit
# Changes applied to working tree, not committed yet
# Useful when you want to modify before committing
# ─── Cherry-pick multiple commits ───
git cherry-pick abc1234 def5678 ghi9012
# ─── Cherry-pick a range ───
git cherry-pick abc1234..ghi9012
# Applies commits AFTER abc1234 up to ghi9012
# ─── If cherry-pick conflicts ───
# Resolve the conflict, then:
git add .
git cherry-pick --continue
# ─── Abort a cherry-pick ───
git cherry-pick --abort
# ─── Recovery: cherry-pick from reflog ───
git reflog
# Find: abc1234 HEAD@{5}: commit: Important fix
git cherry-pick abc1234Key Takeaways
- Cherry-pick copies one commit to your current branch — creating a new commit with a new hash.
- Use it for targeted fixes, selective recovery, and backporting.
- The source branch is never modified.
- Conflicts are possible and resolved the same way as merge conflicts.
Interview Prep
-
Q: What does
git cherry-pickdo? A: It takes a specific commit by SHA and replays its changes on the current branch, creating a new commit with the same diff but a new hash and parent. The source branch remains unchanged. -
Q: When would you use cherry-pick instead of merge? A: When you need only one or a few specific commits from another branch, not the entire branch history. Common scenarios include applying a hotfix from develop to a release branch, or rescuing a good commit from an otherwise messy branch.
-
Q: Can cherry-pick cause conflicts? A: Yes. If the current branch has changes in the same lines that the cherry-picked commit modifies, Git will report a conflict. Resolve it the same way as a merge conflict — edit the files,
git add, andgit cherry-pick --continue.