git cherry-pick allows you to apply a specific commit from one branch to another, without merging the entire branch. It's like picking a cherry 🍒 from a Git tree 🌳!
Why Cherry-Pick is Awesome
- Express hotfixes: Fix urgent issues without merging everything
- Small features: Add an isolated functionality
- Fewer conflicts: No complex merge operations
- Clean history: No unnecessary merge commits
- Surgical precision: "I want JUST THIS fix!"
Common Use Cases
1. Production Hotfix
Scenario: You've fixed a critical bug in the develop branch, but production needs the fix NOW without all the other development changes.
Solution
- Identify the bug fix commit in
develop - Switch to
mainorproductionbranch - Cherry-pick just that one commit
- Deploy the fix immediately
2. Backport Feature to Release Branch
Scenario: A client needs a specific feature from develop in the current release, but the full branch isn't ready.
3. Revert Wrong Merge
Scenario: After merging a feature branch, you realize only some commits should have been included.
4. Recover Lost Commits
Scenario: A branch was accidentally deleted or reset, but you need specific commits from it.
Essential Commands
Basic Cherry-Pick Workflow
Step-by-Step
# 1. Fetch latest changes git fetch origin # 2. View the commit you want to pick git show abc123d # 3. Switch to target branch git checkout main # 4. Apply the commit git cherry-pick abc123d # 5. Push the changes git push origin main
Advanced Options
| Command | Purpose |
|---|---|
git cherry-pick abc123d | Pick a single commit |
git cherry-pick abc123d..def456g | Pick a range of commits |
git cherry-pick -n abc123d | Apply without committing (--no-commit) |
git cherry-pick -x abc123d | Add "cherry picked from" note |
git cherry-pick --edit abc123d | Edit commit message before applying |
git cherry-pick --continue | Continue after resolving conflicts |
git cherry-pick --abort | Cancel cherry-pick operation |
git cherry-pick --skip | Skip current commit and continue |
Handling Conflicts
Conflicts can occur when the target branch has diverged from the source. Here's how to resolve them:
Conflict Resolution Workflow
When Conflicts Occur
# 1. Cherry-pick encounters conflict git cherry-pick abc123d # CONFLICT (content): Merge conflict in src/app.js # 2. Check status to see conflicted files git status # 3. Open and resolve conflicts manually # Look for <<<<<<< HEAD markers in files # 4. After resolving, stage the files git add src/app.js # 5. Continue cherry-pick git cherry-pick --continue # Alternative: Abort if you change your mind git cherry-pick --abort
Conflict Markers Explained
<<<<<<< HEAD (Current branch) const API_URL = 'https://production.api.com'; ======= const API_URL = 'https://staging.api.com'; >>>>>>> abc123d (Cherry-picked commit) Choose one, combine, or write new code, then remove markers.
Complete Example: Production Hotfix
Situation: A critical security bug was fixed in develop, but main needs the fix without the new features.
Complete Workflow
# 1. Find the security fix commit in develop git checkout develop git log --oneline --grep="security fix" # Found: a1b2c3d Fix XSS vulnerability in user input # 2. View the commit to verify it's the right one git show a1b2c3d # 3. Switch to main branch git checkout main # 4. Make sure main is up to date git pull origin main # 5. Cherry-pick the security fix git cherry-pick a1b2c3d # 6. Run tests to ensure nothing broke npm test # 7. Push to production git push origin main # 8. Tag the security fix for tracking git tag -a security-fix-v1.2.1 -m "Security: Fixed XSS vulnerability" git push origin security-fix-v1.2.1
Cherry-Picking Multiple Commits
Method 1: Range Selection
# Pick all commits from abc123d to def456g (exclusive of abc123d) git cherry-pick abc123d..def456g # Pick all commits including abc123d (inclusive) git cherry-pick abc123d^..def456g
Method 2: Multiple Individual Commits
# Pick specific commits (in order) git cherry-pick a1b2c3d e4f5g6h i7j8k9l # Pick with custom messages git cherry-pick -x a1b2c3d e4f5g6h i7j8k9l
Best Practices
DO ✅
- Always verify the commit first: Use
git showbefore cherry-picking - Use -x flag for traceability: Adds reference to original commit
- Test after cherry-picking: Run your test suite to catch issues
- Cherry-pick small commits: Easier to manage and less prone to conflicts
- Document why you cherry-picked: Add context in commit message
- Keep branches in sync: Eventually merge to avoid divergence
DON'T ❌
- Don't cherry-pick large features: Use merge instead
- Don't cherry-pick merge commits: Can cause duplicate history
- Don't overuse cherry-pick: Consider if merge or rebase is better
- Don't cherry-pick without understanding: Know what the commit changes
- Don't ignore conflicts: Resolve carefully, don't rush
- Don't cherry-pick public commits repeatedly: Creates duplicate history
Common Pitfalls and Solutions
Pitfall 1: Duplicate Commits
Problem: Cherry-picked commits appear twice in history after merging branches.
Solution: Use git merge -X theirs or rebase to avoid duplicates, or accept them and use git log --cherry-mark to identify.
Pitfall 2: Dependencies Between Commits
Problem: Cherry-picking a commit that depends on previous commits causes errors.
Solution: Cherry-pick the range of dependent commits, or identify and include dependencies.
Pitfall 3: Wrong Commit Selected
Problem: Picked the wrong commit hash.
Solution: Use git cherry-pick --abort if not pushed yet, or git revert if already pushed.
Cherry-Pick vs Merge vs Rebase
| Aspect | Cherry-Pick | Merge | Rebase |
|---|---|---|---|
| Use case | Specific commits | Entire branches | Rewrite history |
| History | Creates new commit | Merge commit | Linear history |
| Safety | Safe (new commit) | Very safe | Risky (rewrites) |
| Conflicts | Per commit | Once | Per commit |
| Best for | Hotfixes, specific features | Feature integration | Clean up before merge |
Useful Tips and Tricks
Find Commits to Cherry-Pick
# Search by message git log --all --oneline --grep="bug fix" # Find commits by author git log --author="John Doe" --oneline # View commits in another branch not in current git log main..develop --oneline # Interactive commit browser git log --all --decorate --oneline --graph
Identify Cherry-Picked Commits
# Mark cherry-picked commits in log git log --cherry-mark --oneline main...develop # Find exact duplicate commits git log --left-right --cherry-pick main...develop
Need Git Workflow Consulting?
Our VOID team can help you implement efficient Git workflows for your team. We work on:
- Git workflow design (GitFlow, trunk-based, etc.)
- Branching strategies for your team size and process
- CI/CD integration with Git
- Team training on advanced Git techniques
- Migration from other version control systems
Additional Resources
- Official Git Cherry-Pick Documentation
- DevOps Expertise: our DevOps services
- All our publications: tech guides and news
Article published on 2025-12-06. Complete guide to git cherry-pick with practical examples and best practices.