Utilisation de Git Stash
Table des matières
- Qu'est-ce que Git Stash ?
- Commandes de base
- Gérer plusieurs stashes
- Options avancées
- Cas d'usage
- Bonnes pratiques
1 - Qu'est-ce que Git Stash ?
git stash permet de sauvegarder temporairement vos modifications sans les commiter.
Pourquoi utiliser stash ?
| Situation | Solution |
|---|---|
| Changement de branche urgent | Stash les modifications |
| Pull avec modifications locales | Stash, pull, pop |
| Tester du code propre | Stash temporairement |
| Garder du travail en cours | Stash nommé |
Comment ça marche ?
Working Directory Stash Stack
──────────────── ───────────
Modifications → stash@{0}
(sauvegardées) stash@{1}
stash@{2}
...
🔝 Retour à la table des matières
2 - Commandes de base
Stash les modifications
git stash
# ou
git stash push
Voir les stashes
git stash list
Résultat :
stash@{0}: WIP on main: abc1234 Dernier commit
stash@{1}: WIP on feature: def5678 Feature WIP
Récupérer les modifications
# Appliquer et supprimer le stash
git stash pop
# Appliquer sans supprimer
git stash apply
Supprimer un stash
# Supprimer le dernier
git stash drop
# Supprimer tous les stashes
git stash clear
🔝 Retour à la table des matières
3 - Gérer plusieurs stashes
Stash avec un message
git stash push -m "WIP: login feature"
git stash push -m "Debug: testing API"
Résultat de git stash list :
stash@{0}: On main: Debug: testing API
stash@{1}: On main: WIP: login feature
Appliquer un stash spécifique
# Par index
git stash apply stash@{1}
git stash pop stash@{1}
# Par numéro
git stash apply 1
git stash pop 1
Voir le contenu d'un stash
# Résumé
git stash show
# Diff complet
git stash show -p
git stash show -p stash@{1}
Supprimer un stash spécifique
git stash drop stash@{1}
🔝 Retour à la table des matières
4 - Options avancées
Stash avec les fichiers non suivis
Par défaut, stash ignore les fichiers untracked :
# Inclure les fichiers non suivis
git stash push -u
git stash push --include-untracked
# Inclure TOUT (même les fichiers ignorés)
git stash push -a
git stash push --all
Stash de fichiers spécifiques
# Stash seulement certains fichiers
git stash push -m "Stash partiel" file1.txt file2.txt
# Stash interactif (choisir les hunks)
git stash push -p
Créer une branche depuis un stash
git stash branch nouvelle-branche
# Équivalent à :
# git checkout -b nouvelle-branche
# git stash pop
Appliquer sur une branche différente
# Stash sur main
git stash push -m "Mon travail"
# Aller sur feature
git checkout feature/login
# Appliquer le stash
git stash pop
🔝 Retour à la table des matières
5 - Cas d'usage
Cas 1 : Changement de branche urgent
# Vous travaillez sur feature/login
# Un bug urgent sur main !
git stash push -m "WIP login"
git checkout main
# Corriger le bug...
git commit -am "fix: urgent bug"
git push
# Revenir au travail
git checkout feature/login
git stash pop
Cas 2 : Pull avec modifications locales
# Vous avez des modifications non commitées
git pull origin main
# error: cannot pull with rebase: You have unstaged changes
# Solution
git stash
git pull origin main
git stash pop
Cas 3 : Tester du code propre
# Sauvegarder les modifications
git stash
# Tester la version propre
npm test
# Récupérer les modifications
git stash pop
Cas 4 : Déplacer du travail vers une autre branche
# Oups, j'ai travaillé sur main au lieu de feature !
git stash push -m "Travail à déplacer"
git checkout -b feature/correct-branch
git stash pop
🔝 Retour à la table des matières
6 - Bonnes pratiques
✅ À faire
| Pratique | Raison |
|---|---|
| Utiliser des messages | Retrouver facilement |
| Stash court | Éviter les conflits |
| Vérifier avant pop | Savoir ce qu'on récupère |
❌ À éviter
| Pratique | Risque |
|---|---|
| Stash longue durée | Oubli, conflits |
| Stash au lieu de commit | Perte de travail |
| Trop de stashes | Confusion |
Workflow recommandé
# 1. Sauvegarder avec un message clair
git stash push -m "WIP: description claire"
# 2. Faire ce qu'il y a à faire
git checkout other-branch
# ...
# 3. Revenir et récupérer
git checkout original-branch
git stash list # Vérifier
git stash pop
Résumé des commandes
| Commande | Description |
|---|---|
git stash | Sauvegarder les modifications |
git stash push -m "msg" | Avec un message |
git stash list | Voir tous les stashes |
git stash show | Voir le contenu |
git stash pop | Récupérer et supprimer |
git stash apply | Récupérer sans supprimer |
git stash drop | Supprimer un stash |
git stash clear | Supprimer tous les stashes |
git stash push -u | Inclure les fichiers untracked |