Lo que lograrás
Crearás un worktree de Git para hacer checkout de un segundo branch en un directorio diferente mientras mantienes tu trabajo actual intacto.
El problema
Estás profundo en feature/checkout cuando un compañero te pinga sobre un hotfix urgente en main.
Paso 1: examinar el repo existente
cd ~/work/myproject
git worktree list
Paso 2: añadir un worktree para el hotfix
git worktree add ../myproject-hotfix main
Paso 3: crear un nuevo branch en el worktree
cd ../myproject-hotfix
git checkout -b hotfix/cve-2026-1234
git worktree add -b hotfix/cve-2026-1234 ../myproject-hotfix main
Paso 4: trabajar en paralelo
cd ../myproject-hotfix
git push -u origin hotfix/cve-2026-1234
cd ../myproject
Paso 5: listar e inspeccionar
git worktree list
Paso 6: limpieza
git worktree remove ../myproject-hotfix
git worktree prune
Cómo funciona
El .git del repo original es el único object store.
Casos de uso
Revisar PR de un colega
gh pr checkout 123 --branch pr-123
git worktree add ../pr-123 pr-123
cd ../pr-123
npm install
npm test
Comparar dos branches release
git worktree add ../release-1.4 release/1.4
git worktree add ../release-1.5 release/1.5
diff -ru ../release-1.4/src ../release-1.5/src
Builds CI de larga duración
git worktree add ./build-linux $SHA
git worktree add ./build-windows $SHA
Backups antes de operaciones destructivas
git worktree add ../backup HEAD
git rebase -i HEAD~20
Limitaciones
- El mismo branch no puede estar en dos worktrees.
- Los hooks pueden comportarse mal.
- Los submódulos pueden interactuar extrañamente.
- Algunos IDEs no manejan bien múltiples worktrees.
Configuración específica de worktree
git -C ../hotfix config core.editor "vim"
Alias
[alias]
wt = worktree
wta = worktree add
wtl = worktree list
wtr = worktree remove