Gérer les conflits de Cherry-pick
Parfois, lorsque vous faites un cherry-pick d'un commit, Git peut rencontrer des conflits si les modifications du commit entrent en conflit avec les modifications de votre branche actuelle. Dans cette étape, nous allons apprendre à gérer les conflits de cherry-pick et à annuler une opération de cherry-pick.
Création d'un scénario avec des conflits potentiels
Tout d'abord, créons un scénario qui entraînera un conflit de cherry-pick :
## Switch to main branch and modify README.md
git checkout main
echo -e "## Cherry Pick Lab\n\nThis is the main branch README." > README.md
git commit -am "Update README in main branch"
## Switch to feature branch and make a conflicting change to README.md
git checkout feature-branch
echo -e "## Cherry Pick Lab\n\nThis README has been updated in the feature branch." > README.md
git commit -am "Update README in feature branch"
Tentative de Cherry-pick avec des conflits
Maintenant, revenons à la branche principale et essayons de faire un cherry-pick du commit de la branche feature :
git checkout main
CONFLICT_HASH=$(git log feature-branch --oneline | grep "Update README in feature" | cut -d ' ' -f 1)
git cherry-pick $CONFLICT_HASH
Étant donné que les deux branches ont modifié les mêmes lignes dans README.md, vous devriez voir un conflit :
error: could not apply a1b2c3d... Update README in feature branch
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Visualisation du conflit
Examinons le conflit :
git status
Vous devriez voir une sortie indiquant un conflit dans README.md :
On branch main
You are currently cherry-picking commit a1b2c3d.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Regardons le contenu du fichier en conflit :
cat README.md
Vous devriez voir quelque chose comme :
## Cherry Pick Lab
<<<<<<< HEAD
This is the main branch README.
=======
This README has been updated in the feature branch.
>>>>>>> a1b2c3d... Update README in feature branch
Résolution du conflit
Pour résoudre le conflit, nous devons modifier le fichier et décider quelles modifications conserver. Modifions README.md pour inclure les deux modifications :
echo -e "## Cherry Pick Lab\n\nThis is the main branch README.\n\nThis README has also been updated with content from the feature branch." > README.md
Maintenant, marquons le conflit comme résolu et continuons le cherry-pick :
git add README.md
git cherry-pick --continue
Git ouvrira un éditeur avec un message de commit par défaut. Enregistrez et fermez l'éditeur pour terminer le cherry-pick.
Annulation d'un Cherry-pick
Parfois, vous pouvez décider de ne pas vouloir résoudre les conflits et préféreriez annuler l'opération de cherry-pick. Créons un autre conflit, puis annulons le cherry-pick :
## Create another conflicting commit in feature branch
git checkout feature-branch
echo "// This will conflict with app.js in main" > app.js
git commit -am "Modify app.js in feature branch"
## Try to cherry-pick this commit to main
git checkout main
ANOTHER_CONFLICT=$(git log feature-branch --oneline | grep "Modify app.js" | cut -d ' ' -f 1)
git cherry-pick $ANOTHER_CONFLICT
Vous devriez voir un autre conflit. Cette fois, annulons le cherry-pick :
git cherry-pick --abort
Vous devriez voir que l'opération de cherry-pick a été annulée et que votre répertoire de travail a été restauré à son état précédent :
git status
Output:
On branch main
nothing to commit, working tree clean
La gestion des conflits lors des opérations de cherry-pick est une compétence essentielle pour les utilisateurs de Git. Vous avez trois options lorsque vous rencontrez un conflit :
- Résoudre le conflit manuellement, puis utiliser
git add et git cherry-pick --continue
- Ignorer le commit en conflit avec
git cherry-pick --skip
- Annuler l'ensemble de l'opération de
cherry-pick avec git cherry-pick --abort
La meilleure approche dépend de la situation spécifique et des exigences de votre projet.