WTF is `git cherry-pick`?
Written by Namit Jain·July 1, 2025·2 min read
You’ve got a hotfix chilling on dev
that must land on main
pronto. But merging dev
would drag in half-baked features and who knows what else. Enter git cherry-pick
your surgical code transplant tool.
What actually happens?
Run:
git checkout main
git cherry-pick <commit-hash>
Git grabs the diff from <commit-hash>
and replays it on main
as a brand-new commit. Same content, fresh identity.
Why you need this
- Hotfix emergencies: pluck that one bugfix without the noise.
- Selective feature moves: steal just the feature you want.
- History hacks: clean up messy branches by cherry-picking verified commits.
Anatomy of the output
[main 5f8d3e2] Fix typo in README
Author: teammate <teammate@example.com>
Date: Fri Jul 4 17:22:46 2025 +0530
5f8d3e2
: new commit hash no shady reuse.- Message & author carried over to keep context.
Conflict mode: activate
Conflicts are inevitable. When they hit:
- Fix markers (
<<<<
,====
,>>>>
) in your favorite editor. git add <resolved-files>
git cherry-pick --continue
Spooked? Reset with:
git cherry-pick --abort
Back to pre-cherry-pick bliss.
Pro tips
- Cherry-pick small, self-contained commits large ones invite chaos.
- Avoid cherry-picking merge commits unless you enjoy pain.
- Push immediately after your successful pick don’t ghost your teammates.
- Always ask: “Merge or cherry-pick?” If you need more than one commit, merging might be smarter.
Quick scenario
-
Team fixes a critical bug on
dev
. -
Stakeholders demand it on
main
right now. -
Other dev commits? Unstable, unreviewed—thanks but no thanks.
-
You:
git checkout main git cherry-pick abc1234
-
Customers: happy.
-
You: legend.
Interactive demo
ecommerce-app
Current branch:main
Reset repository to initial state
Switch to main branch
Switch to develop branch
Switch to hotfix branch
What to do here?
Repository Branches
Click on commits from other branches to cherry-pick them
main
def4562024-01-10
Add user authentication system
alice • 2 file(s)
abc1232024-01-01
Initial commit
system • 1 file(s)
develop
jkl0122024-01-16
Add experimental payment feature
charlie • 2 file(s)
ghi7892024-01-15
Fix authentication bypass vulnerability
bob • 1 file(s)
def4562024-01-10
Add user authentication system
alice • 2 file(s)
abc1232024-01-01
Initial commit
system • 1 file(s)
hotfix
mno3452024-01-17
Critical security patch for auth
security-team • 1 file(s)
def4562024-01-10
Add user authentication system
alice • 2 file(s)
abc1232024-01-01
Initial commit
system • 1 file(s)
Terminal
Type 'help' for available commands
Initialized git repository for ecommerce-app
Currently on branch: main
$
TL;DR
- What: replay one commit onto another branch.
- How:
git cherry-pick <hash>
. - Why: hotfixes, targeted changes, branch surgery.