Ce que vous accomplirez
Vous créerez un worktree Git pour checker une seconde branche dans un répertoire différent tout en gardant votre travail actuel intact.
Le problème
Vous êtes profond dans feature/checkout quand un coéquipier vous ping sur un hotfix urgent dans main.
Étape 1 : examiner le repo existant
cd ~/work/myproject
git worktree list
Étape 2 : ajouter un worktree pour le hotfix
git worktree add ../myproject-hotfix main
Étape 3 : créer une nouvelle branche dans le worktree
cd ../myproject-hotfix
git checkout -b hotfix/cve-2026-1234
git worktree add -b hotfix/cve-2026-1234 ../myproject-hotfix main
Étape 4 : travailler en parallèle
cd ../myproject-hotfix
git push -u origin hotfix/cve-2026-1234
cd ../myproject
Étape 5 : lister et inspecter
git worktree list
Étape 6 : nettoyage
git worktree remove ../myproject-hotfix
git worktree prune
Comment ça marche
Le .git du repo original est le seul object store.
Cas d'usage
Relire le PR d'un collègue
gh pr checkout 123 --branch pr-123
git worktree add ../pr-123 pr-123
cd ../pr-123
npm install
npm test
Comparer deux 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 longue durée
git worktree add ./build-linux $SHA
git worktree add ./build-windows $SHA
Backups avant opérations destructrices
git worktree add ../backup HEAD
git rebase -i HEAD~20
Limitations
- La même branche ne peut être dans deux worktrees.
- Les hooks peuvent mal se comporter.
- Les submodules peuvent interagir étrangement.
- Certains IDEs ne gèrent pas bien plusieurs worktrees.
Configuration spécifique au worktree
git -C ../hotfix config core.editor "vim"
Alias
[alias]
wt = worktree
wta = worktree add
wtl = worktree list
wtr = worktree remove