🍒 Pick Commits Like Cherries
Need one commit from another branch? Don’t merge everything. Cherry-pick copies specific commits to your current branch. Surgical precision.
The Problem
# Scenario: Bug fix on wrong branch git branch * feature-login main hotfix # You committed bug fix to feature-login # But it needs to be on hotfix branch too! git log --oneline a1b2c3d Fix critical security bug ← Need this on hotfix! d4e5f6g Add login form g7h8i9j Update styles # Don't want to merge entire feature branch # Just need the security fix
Cherry-Pick Solution
# Switch to target branch git checkout hotfix # Cherry-pick the commit git cherry-pick a1b2c3d # Done! Commit copied to hotfix branch git log --oneline a1b2c3d Fix critical security bug ← Same commit, now on hotfix! x7y8z9a Previous hotfix commit
🎯 Common Use Cases
1. Bug Fix on Wrong Branch
# Fixed bug on feature branch, need on main git checkout main git cherry-pick abc123 # Or cherry-pick from another person's branch git cherry-pick origin/their-branch-name~2 # 2 commits ago
2. Apply Multiple Commits
# Cherry-pick range of commits git cherry-pick abc123..def456 # Cherry-pick multiple specific commits git cherry-pick abc123 def456 ghi789 # Cherry-pick all commits from another branch git cherry-pick feature-branch
3. Hotfix to Multiple Versions
# Apply same fix to v1.0, v2.0, v3.0 branches git checkout v1.0-stable git cherry-pick abc123 git checkout v2.0-stable git cherry-pick abc123 git checkout v3.0-stable git cherry-pick abc123 # Same fix on all versions!
Handle Conflicts
git cherry-pick abc123 # If conflicts: # CONFLICT (content): Merge conflict in file.js # Fix conflicts vim file.js # Resolve conflicts # Mark as resolved git add file.js # Continue cherry-pick git cherry-pick --continue # Or abort git cherry-pick --abort
🔧 Advanced Options
# Cherry-pick without committing (stage changes only) git cherry-pick -n abc123 # Make modifications, then commit manually # Cherry-pick and edit commit message git cherry-pick -e abc123 # Add 'cherry picked from' note to commit message git cherry-pick -x abc123 # Adds: (cherry picked from commit abc123) # Cherry-pick but with different author git cherry-pick abc123 git commit --amend --author="John Doe" # Cherry-pick only the changes, not the commit git cherry-pick -n abc123 git reset # Unstage # Now you have changes in working directory
Real-World Workflow
📋 Production Hotfix Workflow
# Bug found in production! # Create hotfix branch from main git checkout main git checkout -b hotfix-critical-bug # Fix bug, commit git commit -m "Fix critical login bug" # Commit hash: xyz789 # Deploy hotfix to production git checkout main git merge hotfix-critical-bug git push # Now cherry-pick to development branch git checkout develop git cherry-pick xyz789 # Now cherry-pick to all active feature branches git checkout feature-new-ui git cherry-pick xyz789 git checkout feature-api-v2 git cherry-pick xyz789 # Same fix everywhere!
✅ When to Use Cherry-Pick
- Bug fixes: Apply to multiple branches/versions
- Backporting: Bring new features to old versions
- Mistakes: Commit on wrong branch, move it
- Selective merging: Want some commits, not all
- Team collaboration: Grab colleague’s commit
⚠️ Caveats
- Creates duplicate commits: Same change, different hash
- Can cause conflicts: If code has diverged
- Loses context: Commit might not make sense alone
- Tracking is harder: Git doesn’t track cherry-picks automatically
- Prefer merge when possible: Cherry-pick is for exceptions
💡 Pro Tips
# Find commits to cherry-pick git log --oneline --graph branch-name # Cherry-pick from reflog (even deleted commits!) git reflog git cherry-pick abc123 # Test before cherry-picking git show abc123 # View commit changes # Cherry-pick merge commit (requires -m) git cherry-pick -m 1 merge-commit-hash # -m 1 means use first parent # Undo cherry-pick git reset --hard HEAD~1 # If not pushed git revert commit-hash # If already pushed
🎯 Cheat Sheet
| Command | Action |
|---|---|
git cherry-pick abc123 |
Apply single commit |
git cherry-pick abc..def |
Apply range of commits |
git cherry-pick -n abc123 |
Don’t commit (stage only) |
git cherry-pick -x abc123 |
Add source note to message |
git cherry-pick --continue |
Continue after conflict |
git cherry-pick --abort |
Cancel cherry-pick |
“Critical security fix needed in 3 production versions simultaneously. Cherry-picked same commit to v1, v2, v3 branches. All versions patched in 10 minutes. Cherry-pick saved the day.”
