Lo que lograrás
Tomarás un branch feature que ha quedado atrás de main, le harás rebase limpio sobre el nuevo tip, resolverás cualquier conflicto que surja, y pushearás el branch rebased de vuelta a tu remote.
¿Por qué rebase, no merge?
Mergear main a tu branch feature produce merge commits extra y enreda la historia. Rebase reproduce tus commits sobre el último main.
Paso 1: configurar un sandbox
mkdir rebase-tutorial && cd rebase-tutorial
git init
echo "v1" > app.txt
git add app.txt && git commit -m "Initial"
git checkout -b feature/login
echo "login" > login.txt
git add login.txt && git commit -m "Add login"
git checkout main
echo "v2" > app.txt
git commit -am "Update app to v2"
Paso 2: rebase
git checkout feature/login
git rebase main
Paso 3: manejar conflictos
git status
git add app.txt
git rebase --continue
git rebase --abort
Paso 4: verificar
git log --oneline --graph
Paso 5: pushear
git push --force-with-lease
Variantes comunes
git fetch origin
git rebase origin/main
git config --global pull.rebase true
git checkout main && git pull
git checkout feature/login && git rebase main
Rebase interactivo
git rebase -i main
Trampas y reglas
- Nunca hagas rebase de branches compartidos.
- Usa
--force-with-lease, no--force. - Resuelve conflictos en el orden que surgen.
- Si los conflictos son abrumadores, aborta y reconsidera.
Recuperación
git reflog
git reset --hard ORIG_HEAD