git cherry-pick
Applying specific commits with git cherry-pick — moving individual commits between branches.
What you'll learn
- How to apply a single commit from one branch to another
- When cherry-pick is useful vs merge or rebase
- Cherry-picking multiple commits and ranges
- Handling cherry-pick conflicts
Concept
What git cherry-pick Does
git cherry-pick takes a commit from one branch and applies it as a new commit on your current branch. This lets you grab individual commits without merging an entire branch.
git checkout main
git cherry-pick abc1234
This takes commit abc1234 (from any branch) and creates a new commit with the same changes on main.
When to Use cherry-pick
Cherry-pick is useful when you need specific commits without bringing everything else:
- Hotfix: a bug fix on
developneeds to go tomainimmediately, without the other unfinished features - Backporting: a fix on the new version needs to be applied to the old version branch
- Selective merge: you want one commit from a colleague's branch but not the rest
- Recovery: a commit ended up on the wrong branch and needs to be moved
Branch A: A---B---C---D---E
Branch B: X---Y---Z
After git cherry-pick C on Branch B:
Branch A: A---B---C---D---E
Branch B: X---Y---Z---C'
C' is a new commit with the same changes as C but a different hash.
Cherry-Picking Multiple Commits
# Cherry-pick specific commits
git cherry-pick abc1234 def5678 ghi9012
# Cherry-pick a range (inclusive, oldest first)
git cherry-pick A..E
# applies B, C, D, E (not A — the range is exclusive on the start)
# Cherry-pick a range (inclusive on both ends)
git cherry-pick A^..E
# applies A, B, C, D, E
The order matters: cherry-pick applies commits in the order you specify (or chronological order for ranges).
Handling Conflicts
If the commit cannot be applied cleanly:
git cherry-pick abc1234
# CONFLICT (content): Merge conflict in login.js
# Resolve the conflict, then:
git add login.js
git cherry-pick --continue
# To skip this commit:
git cherry-pick --skip
# To abort:
git cherry-pick --abort
Cherry-Pick Options
# Apply the commit but do not commit (leave changes staged)
git cherry-pick --no-commit abc1234
# or:
git cherry-pick -n abc1234
# Edit the commit message before committing
git cherry-pick -e abc1234
# Apply the commit with the original author info (not your name)
git cherry-pick -x abc1234
# adds "(cherry picked from commit ...)" to the message
cherry-pick vs merge vs rebase
git merge— brings ALL commits from a branch, with a merge commitgit rebase— replays ALL your commits on top of another branchgit cherry-pick— brings SPECIFIC commits, one at a time
Cherry-pick is the scalpel; merge and rebase are the broad strokes.
The Downside of cherry-pick
Cherry-picking creates duplicate commits (same changes, different hashes). If you later merge the source branch, Git may have trouble recognizing that the changes are already applied, potentially causing conflicts.
Use cherry-pick sparingly — it is a tool for specific situations, not a daily workflow. For routine integration, use merge or rebase.
A Common Pattern: Hotfix
# Bug fix is committed on develop
git checkout develop
git commit -m "fix: urgent security patch"
# Apply the same fix to main immediately
git checkout main
git cherry-pick develop # cherry-picks the last commit on develop
git push origin main
# Later, when develop merges into main, Git may recognize the duplicate
# or you may need to handle it — this is the trade-off of cherry-pickExample
# --- Cherry-pick a hotfix from develop to main ---
git checkout develop
git log --oneline -3
# c3d2e1f fix: security vulnerability in auth
# b2c3d4e feat: add user settings page
# a1b2c3d feat: add profile photos
git checkout main
git cherry-pick c3d2e1f
# applies the security fix to main
git push origin main
# --- Cherry-pick multiple specific commits ---
git cherry-pick a1b2c3d c3d2e1f
# --- Cherry-pick a range of commits ---
git cherry-pick b2c3d4e..c3d2e1f
# applies the commits between (exclusive start)
# --- Cherry-pick without committing ---
git cherry-pick --no-commit abc1234
# changes are staged, you can modify before committing
git commit -m "fix: apply security patch with modifications"
# --- Cherry-pick with conflict resolution ---
git cherry-pick abc1234
# CONFLICT in login.js
# ... resolve in editor ...
git add login.js
git cherry-pick --continue
# --- Abort a problematic cherry-pick ---
git cherry-pick --abort
# --- Cherry-pick and edit the commit message ---
git cherry-pick -e abc1234
# editor opens to change the message
# --- Mark the cherry-picked commit with its origin ---
git cherry-pick -x abc1234
# message includes: "(cherry picked from commit abc1234...)"
Shows cherry-picking a hotfix from develop to main, multiple commits, ranges, --no-commit for staging without committing, conflict resolution, aborting, editing the message, and the -x flag to record the origin. The hotfix pattern is the most common cherry-pick use case.
Try it
- Code Diff
Review a commit's changes before cherry-picking it.
- CI/CD YAML Generator
Cherry-picked hotfixes should trigger CI to verify the fix.
Common mistakes
The mistake
Cherry-picking a commit and later merging the source branch, causing duplicate changes
The fix
Cherry-pick creates a new commit with the same changes but a different hash. If you later merge the source branch, the original commit comes too, potentially causing conflicts or duplicate changes. Use cherry-pick for temporary fixes (like hotfixes) where the source branch will not be merged, or be prepared to handle the duplicate during the later merge.
The mistake
Cherry-picking commits in the wrong order
The fix
Cherry-pick applies commits in the order you specify. If commit B depends on commit A, you must cherry-pick A first, then B. For ranges (A..E), Git applies them in chronological order. If you get conflicts, it may be because a later commit depends on an earlier one you have not applied yet.
Related Resources
Related Cheatsheets
- Git Cheatsheet
cherry-pick, cherry-pick --continue, and --abort commands.
Practice git cherry-pick
3 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.