Il problema risolto dai worktree
Sei a meta feature quando arriva una segnalazione di bug urgente.
Aggiungere un worktree
git worktree add ../hotfix-1234 hotfix/login-redirect
git worktree add -b hotfix/login-redirect ../hotfix-1234 main
Elencare e gestire
git worktree list
git worktree remove ../hotfix-1234
git worktree prune
Come funziona internamente
Il .git nella directory originale e il vero repo. Ogni worktree aggiuntivo ha un file .git che punta a main-repo/.git/worktrees/<name>.
Workflow concreti
Hotfix senza disturbare il proprio lavoro
git worktree add ../hotfix main
cd ../hotfix
git checkout -b hotfix/cve-fix
cd -
git worktree remove ../hotfix
Confronto a lungo termine
git worktree add ../old v2.4.0
diff -ru ../old/src ./src
Revisionare la PR di un collega
gh pr checkout 123 --branch pr-123
git worktree add ../pr-123 pr-123
cd ../pr-123
npm test
CI e worktree
git worktree add ./build-linux $SHA
git worktree add ./build-windows $SHA
Limitazioni
- Non si puo fare checkout dello stesso branch in due worktree.
- Alcuni hook possono comportarsi male.
- I submodule nei worktree possono comportarsi in modo sorprendente.
Alias che aiutano
[alias]
wt = worktree
wtl = worktree list
wta = worktree add
wtr = worktree remove