Git Worktree : Travail en Multi-branches
Table des matières
1 - Qu'est-ce que Git Worktree ?
git worktree permet d'avoir plusieurs working directories liés au même dépôt Git.
Le problème
Normalement, pour travailler sur une autre branche, vous devez :
- Stash vos modifications
- Checkout l'autre branche
- Travailler
- Checkout votre branche
- Pop le stash
La solution Worktree
Avec worktree, vous avez plusieurs dossiers avec différentes branches :
~/projects/
├── mon-repo/ # main
├── mon-repo-feature/ # feature/login
└── mon-repo-hotfix/ # hotfix/urgent
Chaque dossier est une branche différente, et ils partagent le même .git.
🔝 Retour à la table des matières
2 - Créer un worktree
Syntaxe de base
git worktree add <chemin> <branche>
Exemples
# Créer un worktree pour une branche existante
git worktree add ../feature-login feature/login
# Créer un worktree avec nouvelle branche
git worktree add -b hotfix/urgent ../hotfix-urgent main
Ce qui se passe
$ git worktree add ../feature-login feature/login
Preparing worktree (checking out 'feature/login')
HEAD is now at abc1234 Latest commit on feature/login
$ ls ../
mon-repo/ # Votre repo principal
feature-login/ # Le nouveau worktree
Structure résultante
~/projects/
├── mon-repo/
│ ├── .git/ # Le vrai dépôt Git
│ ├── src/
│ └── ...
└── feature-login/
├── .git # Fichier (pas dossier!) pointant vers ../mon-repo/.git
├── src/
└── ...
🔝 Retour à la table des matières
3 - Gérer les worktrees
Lister les worktrees
git worktree list
Résultat :
/home/user/projects/mon-repo abc1234 [main]
/home/user/projects/feature-login def5678 [feature/login]
/home/user/projects/hotfix-urgent ghi9012 [hotfix/urgent]
Supprimer un worktree
# Méthode propre (supprime le dossier et la référence)
git worktree remove ../feature-login
# Si le dossier a été supprimé manuellement
git worktree prune
Déplacer un worktree
git worktree move ../feature-login ../login-feature
Verrouiller un worktree
Empêcher la suppression accidentelle :
git worktree lock ../feature-login
git worktree unlock ../feature-login
🔝 Retour à la table des matières
4 - Cas d'usage
Cas 1 : Hotfix pendant le développement
# Vous travaillez sur feature/login
cd ~/projects/mon-repo
# Un bug urgent ! Créer un worktree pour le hotfix
git worktree add ../hotfix-bug hotfix/urgent-bug
# Travailler sur le hotfix
cd ../hotfix-bug
# ... corriger le bug ...
git commit -am "fix: urgent bug"
git push origin hotfix/urgent-bug
# Revenir au travail principal
cd ../mon-repo
# Votre code feature est toujours là !
# Nettoyer après merge
git worktree remove ../hotfix-bug
Cas 2 : Review de Pull Request
# Vous voulez tester la PR d'un collègue
git fetch origin pull/123/head:pr-123
git worktree add ../review-pr-123 pr-123
# Tester le code
cd ../review-pr-123
npm install
npm test
# Nettoyer
cd ../mon-repo
git worktree remove ../review-pr-123
Cas 3 : Comparer deux versions
# Worktree pour la version actuelle
git worktree add ../version-current main
# Worktree pour une ancienne version
git worktree add ../version-old v1.0.0
# Comparer dans deux fenêtres VS Code
code ../version-current
code ../version-old
Cas 4 : Long build en parallèle
# Main fait un build long
cd ~/projects/mon-repo
npm run build # 10 minutes...
# Pendant ce temps, travailler ailleurs
git worktree add ../quick-fix hotfix/quick
cd ../quick-fix
# Corriger rapidement un bug
git commit -am "fix: quick fix"
git push
🔝 Retour à la table des matières
5 - Bonnes pratiques
Organisation des dossiers
# Structure recommandée
~/projects/
├── mon-projet/ # main (worktree principal)
├── mon-projet-feature/ # feature branch
├── mon-projet-hotfix/ # hotfix branch
└── mon-projet-review/ # PR review
Nommage cohérent
# Convention : repo-branch
git worktree add ../mon-repo-feature-login feature/login
git worktree add ../mon-repo-hotfix-urgent hotfix/urgent
✅ À faire
| Pratique | Raison |
|---|---|
| Supprimer après usage | Éviter l'encombrement |
| Utiliser des chemins relatifs | Portabilité |
| Nommer clairement | Retrouver facilement |
❌ À éviter
| Pratique | Risque |
|---|---|
| Trop de worktrees | Confusion |
| Oublier de supprimer | Espace disque |
| Modifier le même fichier | Conflits |
Résumé des commandes
| Commande | Description |
|---|---|
git worktree add <path> <branch> | Créer un worktree |
git worktree add -b <new-branch> <path> | Créer avec nouvelle branche |
git worktree list | Lister les worktrees |
git worktree remove <path> | Supprimer un worktree |
git worktree prune | Nettoyer les références obsolètes |
git worktree move <src> <dst> | Déplacer un worktree |