Git Worktree

1 min

Tabla de contenidos

  1. ¿Qué es worktree?
  2. ¿Por qué worktree?
  3. Uso práctico
  4. Comandos esenciales

1. ¿Qué es worktree?

Git worktree = tener varias carpetas de trabajo para el MISMO repositorio.

Concepto:

  • 1 repo Git = normalmente 1 carpeta
  • Con worktree = 1 repo, varias carpetas
  • Cada carpeta puede estar en una rama distinta

Analogía: es como tener varios escritorios para el mismo proyecto.

Volver a la tabla de contenidos

2. ¿Por qué worktree?

Problema clásico:

bash
# 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 progress

Solución worktree:

bash
# 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.

Volver a la tabla de contenidos

3. Uso práctico

Preparación de base:

bash
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 worktrees:

bash
# 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 list

Resultado:

demo-worktree/       → main branch
../feature-work/     → feature-new branch  
../hotfix-branch/    → hotfix branch (nueva)

Trabajar en paralelo:

bash
# 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.

Volver a la tabla de contenidos

4. Comandos esenciales

ComandoAcció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 listVer todos los worktrees
git worktree remove <path>Eliminar un worktree
git worktree pruneLimpiar worktrees eliminados

Casos de uso típicos:

1. Hotfix urgente:

bash
git worktree add ../hotfix main
cd ../hotfix && echo "fix" > fix.py
git add . && git commit -m "Hotfix"
git push origin main

2. Revisión de una PR:

bash
git worktree add ../pr-review pr-branch
cd ../pr-review  # Probar la PR sin alterar tu trabajo

3. Comparación de ramas:

bash
git worktree add ../version-a branch-a
git worktree add ../version-b branch-b
# Compara archivos entre ../version-a y ../version-b

Limpieza:

bash
git worktree remove ../feature-work
git worktree remove ../hotfix-branch  
git worktree prune                    # Limpiar las referencias

Worktree = multitarea Git sin compromisos.

Volver a la tabla de contenidos