Aller au contenu principal

Utilisation de Git Stash


Table des matières


  1. Qu'est-ce que Git Stash ?
  2. Commandes de base
  3. Gérer plusieurs stashes
  4. Options avancées
  5. Cas d'usage
  6. Bonnes pratiques


1 - Qu'est-ce que Git Stash ?



git stash permet de sauvegarder temporairement vos modifications sans les commiter.

Pourquoi utiliser stash ?

SituationSolution
Changement de branche urgentStash les modifications
Pull avec modifications localesStash, pull, pop
Tester du code propreStash temporairement
Garder du travail en coursStash 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

PratiqueRaison
Utiliser des messagesRetrouver facilement
Stash courtÉviter les conflits
Vérifier avant popSavoir ce qu'on récupère

❌ À éviter

PratiqueRisque
Stash longue duréeOubli, conflits
Stash au lieu de commitPerte de travail
Trop de stashesConfusion

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

CommandeDescription
git stashSauvegarder les modifications
git stash push -m "msg"Avec un message
git stash listVoir tous les stashes
git stash showVoir le contenu
git stash popRécupérer et supprimer
git stash applyRécupérer sans supprimer
git stash dropSupprimer un stash
git stash clearSupprimer tous les stashes
git stash push -uInclure les fichiers untracked

🔝 Retour à la table des matières