Using Git Stash
Table of contents
- What is Git Stash?
- Basic commands
- Managing multiple stashes
- Advanced options
- Use cases
- Best practices
1 - What is Git Stash?
git stash lets you temporarily save your changes without committing them.
Why use stash?
| Situation | Solution |
|---|---|
| Urgent branch switch | Stash the changes |
| Pull with local changes | Stash, pull, pop |
| Test clean code | Stash temporarily |
| Keep work in progress | Named stash |
How does it work?
Working Directory Stash Stack
──────────────── ───────────
Modifications → stash@{0}
(sauvegardées) stash@{1}
stash@{2}
...
🔝 Back to table of contents
2 - Basic commands
Stash the changes
git stash
# ou
git stash push
View the stashes
git stash list
Result:
stash@{0}: WIP on main: abc1234 Dernier commit
stash@{1}: WIP on feature: def5678 Feature WIP
Retrieve the changes
# Appliquer et supprimer le stash
git stash pop
# Appliquer sans supprimer
git stash apply
Delete a stash
# Supprimer le dernier
git stash drop
# Supprimer tous les stashes
git stash clear
🔝 Back to table of contents
3 - Managing multiple stashes
Stash with a message
git stash push -m "WIP: login feature"
git stash push -m "Debug: testing API"
Output of git stash list:
stash@{0}: On main: Debug: testing API
stash@{1}: On main: WIP: login feature
Apply a specific stash
# Par index
git stash apply stash@{1}
git stash pop stash@{1}
# Par numéro
git stash apply 1
git stash pop 1
View the content of a stash
# Résumé
git stash show
# Diff complet
git stash show -p
git stash show -p stash@{1}
Delete a specific stash
git stash drop stash@{1}
🔝 Back to table of contents
4 - Advanced options
Stash with untracked files
By default, stash ignores untracked files:
# 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 specific files
# Stash seulement certains fichiers
git stash push -m "Stash partiel" file1.txt file2.txt
# Stash interactif (choisir les hunks)
git stash push -p
Create a branch from a stash
git stash branch nouvelle-branche
# Équivalent à :
# git checkout -b nouvelle-branche
# git stash pop
Apply on a different branch
# Stash sur main
git stash push -m "Mon travail"
# Aller sur feature
git checkout feature/login
# Appliquer le stash
git stash pop
🔝 Back to table of contents
5 - Use cases
Case 1: Urgent branch switch
# 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
Case 2: Pull with local changes
# 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
Case 3: Test clean code
# Sauvegarder les modifications
git stash
# Tester la version propre
npm test
# Récupérer les modifications
git stash pop
Case 4: Move work to another branch
# 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
🔝 Back to table of contents
6 - Best practices
✅ Do
| Practice | Reason |
|---|---|
| Use messages | Find them easily |
| Keep stashes short-lived | Avoid conflicts |
| Check before pop | Know what you're recovering |
❌ Avoid
| Practice | Risk |
|---|---|
| Long-lived stash | Forgetting, conflicts |
| Stash instead of commit | Loss of work |
| Too many stashes | Confusion |
Recommended workflow
# 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
Command summary
| Command | Description |
|---|---|
git stash | Save the changes |
git stash push -m "msg" | With a message |
git stash list | View all stashes |
git stash show | View the content |
git stash pop | Retrieve and delete |
git stash apply | Retrieve without deleting |
git stash drop | Delete a stash |
git stash clear | Delete all stashes |
git stash push -u | Include untracked files |