Manejo de Conflictos en Cherry-pick
A veces, cuando haces cherry-pick de un commit, Git puede encontrar conflictos si los cambios en el commit entran en conflicto con los cambios en tu rama actual. En este paso, aprenderemos a manejar los conflictos de cherry-pick y cómo abortar una operación de cherry-pick.
Creando un Escenario con Posibles Conflictos
Primero, creemos un escenario que resultará en un conflicto de cherry-pick:
## Cambiar a la rama principal y modificar 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"
## Cambiar a la rama feature y hacer un cambio en conflicto en 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"
Intentando un Cherry-pick con Conflictos
Ahora, volvamos a la rama principal e intentemos hacer cherry-pick del commit de la rama 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
Dado que ambas ramas modificaron las mismas líneas en README.md, deberías ver un conflicto:
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'
Visualizando el Conflicto
Examinemos el conflicto:
git status
Deberías ver una salida que indica un conflicto en 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")
Veamos el contenido del archivo en conflicto:
cat README.md
Deberías ver algo como:
## 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
Resolviendo el Conflicto
Para resolver el conflicto, necesitamos editar el archivo y decidir qué cambios mantener. Modifiquemos README.md para incluir ambos cambios:
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
Ahora, marquemos el conflicto como resuelto y continuemos el cherry-pick:
git add README.md
git cherry-pick --continue
Git abrirá un editor con un mensaje de commit predeterminado. Guarda y cierra el editor para completar el cherry-pick.
Abortando un Cherry-pick
A veces, podrías decidir que no quieres resolver los conflictos y preferirías cancelar la operación de cherry-pick. Creemos otro conflicto y luego abortemos el 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
Deberías ver otro conflicto. Esta vez, abortemos el cherry-pick:
git cherry-pick --abort
Deberías ver que la operación de cherry-pick ha sido cancelada y tu directorio de trabajo ha sido restaurado a su estado anterior:
git status
Salida:
On branch main
nothing to commit, working tree clean
Manejar los conflictos durante las operaciones de cherry-pick es una habilidad esencial para los usuarios de Git. Tienes tres opciones cuando encuentras un conflicto:
- Resuelve el conflicto manualmente, luego usa
git add y git cherry-pick --continue
- Omite el commit en conflicto con
git cherry-pick --skip
- Aborta toda la operación de
cherry-pick con git cherry-pick --abort
El mejor enfoque depende de la situación específica y de los requisitos de tu proyecto.