git cherry-pick
git cherry-pick
Section titled “git cherry-pick”cherry-pick applies a specific commit (or range of commits) to the current branch, creating a new commit with the same changes but a different hash.
Basic Usage
Section titled “Basic Usage”# Apply a specific commit to the current branchgit cherry-pick abc1234
# Apply multiple commitsgit cherry-pick abc1234 def5678
# Apply a range of commits (exclusive..inclusive)git cherry-pick abc1234..ghi9012Common Use Cases
Section titled “Common Use Cases”Backport a Bug Fix to an Older Release
Section titled “Backport a Bug Fix to an Older Release”# Bug fixed on maingit log --oneline main# abc1234 Fix null pointer in payment processor# ...
# Apply the fix to the v2 release branchgit switch release/v2.0git cherry-pick abc1234Apply a Commit That Went to the Wrong Branch
Section titled “Apply a Commit That Went to the Wrong Branch”# Accidentally committed to main instead of feature branchgit log --oneline main# def5678 Add experimental feature# ...
# Move it to the feature branchgit switch feature/experimentalgit cherry-pick def5678
# Remove from main (if it wasn't pushed yet)git switch maingit reset --hard HEAD~1Pull a Single Fix Without Merging an Entire Branch
Section titled “Pull a Single Fix Without Merging an Entire Branch”# Colleague has a large branch with one critical fix you need nowgit cherry-pick abc1234 # just the fix, not the whole branchCherry-Pick Options
Section titled “Cherry-Pick Options”# Apply but don't commit (leave changes staged)git cherry-pick -n abc1234git cherry-pick --no-commit abc1234
# Cherry-pick and edit the commit messagegit cherry-pick -e abc1234
# Add "cherry picked from commit..." to messagegit cherry-pick -x abc1234
# Continue after resolving conflictsgit cherry-pick --continue
# Abort cherry-pick in progressgit cherry-pick --abort
# Skip a conflicting commit and continue with the restgit cherry-pick --skipHandling Conflicts
Section titled “Handling Conflicts”git cherry-pick abc1234# CONFLICT (content): Merge conflict in src/payment.ts# error: could not apply abc1234...
# 1. Open the conflicting file and resolve conflicts# 2. Stage the resolved filegit add src/payment.ts
# 3. Continuegit cherry-pick --continueCherry-Picking Merge Commits
Section titled “Cherry-Picking Merge Commits”Merge commits have two parents. You must specify which parent represents the mainline:
git cherry-pick -m 1 abc1234 # mainline is parent 1 (usually the branch you merged into)When NOT to Cherry-Pick
Section titled “When NOT to Cherry-Pick”Cherry-pick creates a duplicate commit (same changes, different hash). If the original branch is eventually merged, you’ll have the same changes twice — which can cause confusing conflicts.
Prefer cherry-pick for:
- Specific bug fixes needed on release branches
- Emergency hotfixes
- One-off commits that won’t be merged
Avoid cherry-pick when:
- The whole feature will eventually be merged (just wait for the merge)
- You need many commits from a branch (merge or rebase instead)
Viewing Cherry-Pick Status
Section titled “Viewing Cherry-Pick Status”# See if a commit has been cherry-picked to another branchgit log --oneline main | grep "cherry picked from commit abc1234"
# Or use git log with --cherry-mark to see symmetric differencesgit log --cherry-mark --oneline main...release/v2.0