🍒 Cherry-Pick = Selective Commits
Merging whole branches isn’t always needed. Cherry-pick applies specific commits to any branch.
📝 Cherry-Pick Basics
# Apply single commit git cherry-pick abc123 # Apply multiple commits git cherry-pick abc123 def456 # Cherry-pick range git cherry-pick abc123..def456 # Continue after conflict git cherry-pick --continue # Abort cherry-pick git cherry-pick --abort # Edit commit message git cherry-pick -e abc123 # Keep original author git cherry-pick -x abc123
🎯 Use Cases
# Hotfix to production git checkout main git cherry-pick hotfix-commit # Backport feature git checkout release/v1 git cherry-pick feature-commit # Undo commit git cherry-pick -n abc123 git reset HEAD # Selective merge git log feature --oneline git cherry-pick commit1 commit3 # Multiple branches git checkout branch-b git cherry-pick commit-a commit-b
📝 Advanced Cherry-Pick
# With conflict resolution git cherry-pick abc123 # ... resolve conflicts ... git add . git cherry-pick --continue # Dry run (no-op) git cherry-pick --no-commit abc123 git diff --cached # Cherry-pick with signoff git cherry-pick -s abc123 # Cherry-pick from different repo git fetch another-repo git cherry-pick another-repo/main^..another-repo/main # Multiple commits with no commit git cherry-pick -n abc123 def456 git commit -m "Combined changes" # Cherry-pick merge commits git cherry-pick -m 1 merge-commit # Interactive cherry-pick git cherry-pick -i HEAD~3 # Cherry-pick with edit git cherry-pick -e abc123 # Edit message and save
💡 Cherry-Pick Tips
- Use for hotfixes
- Use for backporting
- Use for release branches
- Be careful with conflicts
- Document cherry-picked commits
Cherry-pick gives you surgical precision with Git commits. It’s essential for maintaining multiple versions.
