Git worktree = tener varias carpetas de trabajo para el MISMO repositorio.
Concepto:
Analogía: es como tener varios escritorios para el mismo proyecto.
Problema clásico:
# Trabajas en feature-A
git checkout feature-A
# Código, código, código... (trabajo sin terminar)
# URGENCIA: bug en main
git stash # Guardar el work in progress
git checkout main # Cambiar a main
# Arreglar el bug...
git checkout feature-A # Volver a tu trabajo
git stash pop # Recuperar el work in progressSolución worktree:
# Trabajas en feature-A en la carpeta principal
# ¿Urgencia? ¡Crea una carpeta nueva para main!
git worktree add ../hotfix main
# Ahora:
# - ./ → feature-A (tu trabajo sigue)
# - ../hotfix → main (para el arreglo urgente)Ventaja: ni stash ni cambio de rama. Solo 2 carpetas, 2 tareas en paralelo.
mkdir demo-worktree && cd demo-worktree
git init
echo "print('main v1')" > app.py
git add . && git commit -m "Main version"
# Crear la rama feature
git checkout -b feature-new
echo "print('feature work')" > feature.py
git add . && git commit -m "Feature work"
git checkout main# Crear un worktree para feature-new
git worktree add ../feature-work feature-new
# Crear un worktree para una rama nueva
git worktree add ../hotfix-branch -b hotfix
# Ver todos los worktrees
git worktree listdemo-worktree/ → main branch
../feature-work/ → feature-new branch
../hotfix-branch/ → hotfix branch (nueva)# Terminal 1: carpeta principal (main)
cd demo-worktree
echo "print('main updated')" >> app.py
git add . && git commit -m "Update main"
# Terminal 2: feature work
cd ../feature-work
echo "print('feature complete')" >> feature.py
git add . && git commit -m "Complete feature"
# Terminal 3: hotfix
cd ../hotfix-branch
echo "print('urgent fix')" > fix.py
git add . && git commit -m "Critical fix"Magia: 3 ramas, 3 carpetas, 0 cambios de contexto.
| Comando | Acción |
|---|---|
git worktree add <path> <branch> | Crear un worktree en una rama existente |
git worktree add <path> -b <new-branch> | Crear un worktree + una rama nueva |
git worktree list | Ver todos los worktrees |
git worktree remove <path> | Eliminar un worktree |
git worktree prune | Limpiar worktrees eliminados |
1. Hotfix urgente:
git worktree add ../hotfix main
cd ../hotfix && echo "fix" > fix.py
git add . && git commit -m "Hotfix"
git push origin main2. Revisión de una PR:
git worktree add ../pr-review pr-branch
cd ../pr-review # Probar la PR sin alterar tu trabajo3. Comparación de ramas:
git worktree add ../version-a branch-a
git worktree add ../version-b branch-b
# Compara archivos entre ../version-a y ../version-bgit worktree remove ../feature-work
git worktree remove ../hotfix-branch
git worktree prune # Limpiar las referenciasWorktree = multitarea Git sin compromisos.