Skip to main content

Using Git Stash


Table of contents


  1. What is Git Stash?
  2. Basic commands
  3. Managing multiple stashes
  4. Advanced options
  5. Use cases
  6. Best practices


1 - What is Git Stash?



git stash lets you temporarily save your changes without committing them.

Why use stash?

SituationSolution
Urgent branch switchStash the changes
Pull with local changesStash, pull, pop
Test clean codeStash temporarily
Keep work in progressNamed 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

PracticeReason
Use messagesFind them easily
Keep stashes short-livedAvoid conflicts
Check before popKnow what you're recovering

❌ Avoid

PracticeRisk
Long-lived stashForgetting, conflicts
Stash instead of commitLoss of work
Too many stashesConfusion
# 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

CommandDescription
git stashSave the changes
git stash push -m "msg"With a message
git stash listView all stashes
git stash showView the content
git stash popRetrieve and delete
git stash applyRetrieve without deleting
git stash dropDelete a stash
git stash clearDelete all stashes
git stash push -uInclude untracked files

🔝 Back to table of contents