Skip to main content

The Git Cherry-pick Command


Table of contents


  1. What is Cherry-pick?
  2. Basic syntax
  3. Use cases
  4. Multiple cherry-pick
  5. Handling conflicts
  6. Best practices


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

AspectMergeCherry-pick
CommitsAll commitsA single commit
HistoryLinkedIndependent
UsageIntegrate a branchCopy 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

OptionDescription
-n or --no-commitDo not commit automatically
-e or --editEdit the message
-xAdd a reference to the original commit
--signoffAdd 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

SituationRecommended
Urgent hotfix in prod
Backport a fix
Recover from a dead branch
Commit on the wrong branch

❌ When to avoid cherry-pick

SituationAlternative
Integrate a complete featuregit merge
Synchronize branchesgit rebase
Repeated useRethink the workflow

Tips

  1. Use -x to keep traceability
  2. Test after each cherry-pick
  3. Avoid cascading cherry-picks (a sign of a bad workflow)
  4. Document why you cherry-pick

Summary

CommandDescription
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..BRange of commits
git cherry-pick --abortAbort
git cherry-pick --continueContinue after a conflict

🔝 Back to table of contents