Git Stash — Save and Restore Work in Progress
In this tutorial, you'll learn about Git Stash. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
What You'll Learn
Use git stash to temporarily save uncommitted work — switch branches without losing changes, manage multiple stashes, and apply selective changes.
Why It Matters
You're in the middle of a feature when a critical bug needs fixing. Stash saves your work-in-progress so you can fix the bug and come right back.
Real-World Use
Pausing work on feature A to fix a production bug, moving changes to a different branch, or saving experimental code without committing.
Basic Stash
# Working on something, but need to switch branches
git status
# modified: login.py
# modified: database.py
# Save it all
git stash
# "Saved working directory and index state WIP on feature: a1b2c3"
# Work on the bug
git checkout main
# ... fix bug, commit, push ...
# Come back
git checkout feature
git stash pop # Restore and remove stash
What Git Stash Does
Before stash:
Working Directory: [login.py (modified), database.py (modified)]
Staging Area: [empty]
After stash:
Working Directory: [clean]
Stash: WIP on feature: modified login.py, database.py
After stash pop:
Working Directory: [login.py (modified), database.py (modified)]
Stash: [empty]
Multiple Stashes
git stash # Stash 0
git stash # Stash 1
git stash # Stash 2
git stash list
# stash@{0}: WIP on feature: last commit msg
# stash@{1}: WIP on main: earlier commit msg
# stash@{2}: WIP on feature: older commit msg
# Apply specific stash
git stash apply stash@{1}
# Drop a specific stash
git stash drop stash@{1}
# Apply and drop (like pop, but for specific stash)
git stash pop stash@{1}
Stash with Message
git stash push -m "WIP: login form validation"
git stash list
# stash@{0}: On feature: WIP: login form validation
Partial Stash
Save only certain files:
# Stash only files matching pattern
git stash push -m "UI changes only" -- *.css *.html
# Stash only unstaged changes (not staged ones)
git stash push -k # --keep-index
# Interactive (choose what to stash hunk by hunk)
git stash push -p
Stash Untracked Files
By default, stash skips untracked and ignored files:
# Include untracked files
git stash --include-untracked # -u
# Include all files (including ignored)
git stash --all # -a
Create a Branch from a Stash
If your stash conflicts with the current branch:
git stash branch new-feature-branch stash@{0}
# Creates a branch starting from the commit where the stash was made
# Applies the stash
# Drops the stash if successful
View Stash Contents
# See what files are changed in a stash
git stash show stash@{0}
# login.py | 15 ++++++++
# database.py | 3 +--
# See the full diff
git stash show -p stash@{0}
# See the diff in a pager
git stash show -v stash@{0}
Stash Workflow
# Typical flow
# 1. Start feature
git checkout -b feature/payments
# 2. Work in progress
echo "initial code" > payment.py
git add payment.py
echo "more changes" >> payment.py # Unstaged
# 3. Need to fix urgent bug
git stash push -m "Payment WIP"
# 4. Fix the bug
git checkout main
git checkout -b hotfix/pricing
# ... fix ...
git commit -m "Fix pricing calculation bug"
git checkout main
git merge hotfix/pricing
git push
# 5. Back to feature
git checkout feature/payments
git stash pop
# Continue where you left off
Common Pitfalls
| Mistake | Fix |
|---|---|
| Stash conflicts on pop | Use git stash branch |
| Lost stash after drop | Use git reflog (limited recovery) |
| Stashed but forgot what's in it | Use git stash show -p |
| Untracked files missing from stash | Add -u or -a |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro