TechLead
Lección 4 de 9
5 min de lectura
Git Avanzado

Cherry-pick y revert

Aplica commits específicos entre ramas y deshace cambios de forma segura con cherry-pick y revert.

Cherry-pick

Cherry-pick aplica commits específicos de una rama a otra, útil para backportear fixes o aplicar cambios selectivos.

Cherry-pick básico

# Apply a single commit
git cherry-pick abc1234

# Apply multiple commits
git cherry-pick abc1234 def5678 ghi9012

# Apply a range of commits
git cherry-pick abc1234^..def5678

# Apply without committing
git cherry-pick -n abc1234

Opciones de cherry-pick

# Edit commit message
git cherry-pick -e abc1234

# Add "cherry picked from" to message
git cherry-pick -x abc1234
# Message includes:
# (cherry picked from commit abc1234...)

# Keep original author
git cherry-pick abc1234  # Author preserved by default

# Sign off
git cherry-pick -s abc1234

Resolución de conflictos

# When conflicts occur
git cherry-pick abc1234
# CONFLICT (content): Merge conflict in file.js

# Resolve conflicts manually
git status
# Edit conflicting files

# Continue after resolving
git add .
git cherry-pick --continue

# Abort cherry-pick
git cherry-pick --abort

# Skip this commit
git cherry-pick --skip

Cherry-pick desde otra rama

# Find commits to cherry-pick
git log --oneline feature-branch

# Cherry-pick to current branch
git checkout main
git cherry-pick feature-branch~3..feature-branch

# Cherry-pick specific file changes
git cherry-pick -n abc1234
git reset HEAD
git add specific-file.js
git commit -m "Cherry-pick specific file"

Revertir commits

Revert crea un nuevo commit que deshace cambios, revirtiendo de forma segura sin reescribir el historial.

Revert básico

# Revert a single commit
git revert abc1234

# Revert without committing
git revert -n abc1234

# Revert multiple commits
git revert abc1234 def5678

# Revert a range (oldest to newest)
git revert abc1234^..def5678

Revertir merges

# Revert a merge commit (must specify parent)
git revert -m 1 merge_commit_hash
# -m 1 means keep changes from first parent (usually main)
# -m 2 means keep changes from second parent (merged branch)

# Check which parent is which
git log --oneline --graph merge_commit_hash^-

Revertir múltiples commits

# Revert last 3 commits
git revert HEAD~3..HEAD

# Revert without creating multiple commits
git revert --no-commit HEAD~3..HEAD
git commit -m "Revert last 3 commits"

# Revert in reverse order to avoid conflicts
git revert HEAD
git revert HEAD~1
git revert HEAD~2

Conflictos al revertir

# When revert conflicts
git revert abc1234
# CONFLICT...

# Resolve conflicts
git status
# Edit files

git add .
git revert --continue

# Or abort
git revert --abort

Deshacer un revert

# Revert the revert!
git revert revert_commit_hash

# Or reset if not pushed
git reset --hard HEAD~1

Casos de uso: Cherry-pick vs Revert

Usa Cherry-pick cuando Usa Revert cuando
Backport de fixes Deshacer cambios ya publicados
Mover commits entre ramas Revertir features
Aplicar cambios selectivos Preservar integridad del historial
Crear hotfixes Colaboración en equipo

Ejemplos prácticos

# Backport a bugfix to release branch
git checkout release/1.0
git cherry-pick main~2  # Apply fix from main

# Roll back a feature in production
git checkout main
git revert feature-commit-hash
git push origin main

# Apply hotfix to multiple versions
for branch in release/1.0 release/1.1 release/2.0; do
  git checkout $branch
  git cherry-pick hotfix-commit
done

Buenas prácticas

  • Usa -x con cherry-pick para rastrear el origen
  • Revertir en lugar de reset en ramas compartidas
  • Prueba bien los commits cherry-picked
  • Documenta los cherry-picks en los mensajes de commit
  • Considera conflictos al cherry-pickear commits antiguos

Continuar aprendiendo