The Git Cherry-pick Command
Table of contents
1 - What is Cherry-pick?
git cherry-pick lets you copy a specific commit from one branch to another.
Visualization
Avant:
A---B---C feature
/
D---E---F main
Après cherry-pick de B sur main:
A---B---C feature
/
D---E---F---B' main
Commit B is copied (not moved) to main with a new hash (B').
Difference from merge
| Aspect | Merge | Cherry-pick |
|---|---|---|
| Commits | All commits | A single commit |
| History | Linked | Independent |
| Usage | Integrate a branch | Copy a specific commit |
🔝 Back to table of contents
2 - Basic syntax
Cherry-pick a commit
git cherry-pick <commit-hash>
Example
# Trouver le commit à cherry-pick
git log feature/login --oneline
# abc1234 fix: critical bug fix
# def5678 feat: add login
# ghi9012 Initial setup
# Aller sur main
git checkout main
# Cherry-pick le fix
git cherry-pick abc1234
Common options
| Option | Description |
|---|---|
-n or --no-commit | Do not commit automatically |
-e or --edit | Edit the message |
-x | Add a reference to the original commit |
--signoff | Add your signature |
With the -x option
git cherry-pick -x abc1234
Resulting message:
fix: critical bug fix
(cherry picked from commit abc1234...)
🔝 Back to table of contents
3 - Use cases
Case 1: Hotfix in production
A bug is fixed on develop but must be applied to main immediately:
# Le fix est sur develop
git log develop --oneline
# abc1234 fix: security vulnerability
# Appliquer sur main (production)
git checkout main
git cherry-pick abc1234
git push origin main
Case 2: Recover a commit from an abandoned branch
# La branche experiment a un bon commit
git log experiment --oneline
# abc1234 feat: useful feature
# def5678 broken stuff
# ghi9012 more broken stuff
# Récupérer seulement le bon commit
git checkout main
git cherry-pick abc1234
Case 3: Backport to an older version
# Fix sur main (v3)
git log main --oneline
# abc1234 fix: important fix
# Appliquer sur la branche de maintenance v2
git checkout release/v2
git cherry-pick abc1234
Case 4: Reorganize commits
# Vous avez commité sur la mauvaise branche
git checkout feature/correct-branch
git cherry-pick abc1234
# Puis supprimer de la mauvaise branche
git checkout feature/wrong-branch
git reset --hard HEAD~1
🔝 Back to table of contents
4 - Multiple cherry-pick
Several individual commits
git cherry-pick abc1234 def5678 ghi9012
Range of commits
# De A à C (A non inclus, C inclus)
git cherry-pick A..C
# De A à C (A inclus)
git cherry-pick A^..C
Without committing (staging only)
git cherry-pick -n abc1234
git cherry-pick -n def5678
git cherry-pick -n ghi9012
# Un seul commit pour tout
git commit -m "feat: combined features from other branch"
Concrete example
# Récupérer les 3 derniers commits de feature
git log feature --oneline
# abc1234 commit 3
# def5678 commit 2
# ghi9012 commit 1
# jkl3456 base
# Cherry-pick tous
git checkout main
git cherry-pick ghi9012 def5678 abc1234
# Ou avec la plage
git cherry-pick jkl3456..abc1234
🔝 Back to table of contents
5 - Handling conflicts
When does a conflict occur?
When the code has evolved differently on the two branches.
git cherry-pick abc1234
# CONFLICT (content): Merge conflict in file.txt
Resolve the conflicts
# 1. Voir les fichiers en conflit
git status
# 2. Éditer les fichiers
code file.txt
# 3. Marquer comme résolu
git add file.txt
# 4. Continuer le cherry-pick
git cherry-pick --continue
Abort the cherry-pick
git cherry-pick --abort
Skip a commit (during a multi-pick)
git cherry-pick --skip
🔝 Back to table of contents
6 - Best practices
✅ When to use cherry-pick
| Situation | Recommended |
|---|---|
| Urgent hotfix in prod | ✅ |
| Backport a fix | ✅ |
| Recover from a dead branch | ✅ |
| Commit on the wrong branch | ✅ |
❌ When to avoid cherry-pick
| Situation | Alternative |
|---|---|
| Integrate a complete feature | git merge |
| Synchronize branches | git rebase |
| Repeated use | Rethink the workflow |
Tips
- Use
-xto keep traceability - Test after each cherry-pick
- Avoid cascading cherry-picks (a sign of a bad workflow)
- Document why you cherry-pick
Summary
| Command | Description |
|---|---|
git cherry-pick <hash> | Copy a commit |
git cherry-pick -x <hash> | With a reference to the original commit |
git cherry-pick -n <hash> | Without committing |
git cherry-pick A..B | Range of commits |
git cherry-pick --abort | Abort |
git cherry-pick --continue | Continue after a conflict |