Da Anonimo (non verificato) , 29 Aprile 2026

Cosa otterrai

Creerai un worktree Git per fare checkout di un secondo branch in una directory diversa mentre mantieni intatto il lavoro corrente.

Il problema

Sei deep in feature/checkout quando un compagno ti pinga su un hotfix urgente.

Passo 1: esaminare il repo esistente

cd ~/work/myproject
git worktree list

Passo 2: aggiungere un worktree per l'hotfix

git worktree add ../myproject-hotfix main

Passo 3: creare un nuovo branch nel worktree

cd ../myproject-hotfix
git checkout -b hotfix/cve-2026-1234
git worktree add -b hotfix/cve-2026-1234 ../myproject-hotfix main

Passo 4: lavorare in parallelo

cd ../myproject-hotfix
git push -u origin hotfix/cve-2026-1234

cd ../myproject

Passo 5: elencare e ispezionare

git worktree list

Passo 6: pulire

git worktree remove ../myproject-hotfix
git worktree prune

Come funziona

Il .git del repo originale e l'unico object store. Ogni worktree aggiuntivo ha un piccolo file .git che punta a main-repo/.git/worktrees/<name>/.

Casi d'uso

Revisionare la PR di un collega

gh pr checkout 123 --branch pr-123
git worktree add ../pr-123 pr-123
cd ../pr-123
npm install
npm test

Confrontare due branch di 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

Build CI di lunga durata

git worktree add ./build-linux $SHA
git worktree add ./build-windows $SHA

Backup prima di operazioni distruttive

git worktree add ../backup HEAD
git rebase -i HEAD~20

Limitazioni

  • Lo stesso branch non puo essere in due worktree.
  • Gli hook possono comportarsi male.
  • I submodule possono interagire stranamente.

Configurazione specifica del worktree

git -C ../hotfix config core.editor "vim"

Alias

[alias]
    wt = worktree
    wta = worktree add
    wtl = worktree list
    wtr = worktree remove