Sometimes you’re in the middle of writing code, and suddenly you need to:
- switch branches to fix a bug,
- pull the latest changes, or
- do something quickly without committing unfinished work.
But your current changes are not ready to commit yet.
This is exactly where Git Stash helps.
Git Stash lets us temporarily save our uncommitted changes and return to a clean working directory—without losing anything.
1. What Is Git Stash?
Git stash is like taking a “snapshot” of our current unfinished work and putting it in a temporary storage area called the stash stack.
Think of it as:
- a clipboard for code
- a place to save our messy work-in-progress
- a way to switch tasks without committing junk
When we’re ready, we can bring those changes back.
2. When Should We Use Git Stash?
Use git stash when:
- We need to change branches but have uncommitted work.
- We want to test something without losing what we’re doing.
- We need a clean working directory (e.g., before pulling).
- Our work isn’t ready to commit yet.
3. Stashing Our Work
Stash all tracked file changes
git stash
Stash everything (tracked + untracked files)
git stash -u
or
git stash --include-untracked
Add a message (recommended)
git stash save "WIP: login page fixes"
or with newer syntax:
git stash push -m "WIP: login page fixes"
4. Viewing All Stashes
To see everything you’ve saved:
git stash list
You will see entries like:
stash@{0}: WIP on main: login page fixes
stash@{1}: WIP on feature-auth: token update
5. Getting Our Work Back
There are two ways to apply stashes.
Option 1: Apply stash and keep it in the stack
git stash apply stash@{0}
Use this when we want to reuse the stash later.
Option 2: Apply stash and remove it from stack
git stash pop stash@{0}
This is usually what beginners need—apply + delete.
6. Deleting Stashes
Remove a specific stash
git stash drop stash@{1}
Clear ALL stashes
(Be careful!)
git stash clear
7. Stashing Only Specific Files
We don’t have to stash everything.
You can stash just one file or a set of files:
git stash push -m "partial stash" file1.js file2.css
8. Stashing With a “Patch” (Interactive Mode)
Want to stash only part of a file?
git stash push -p
We’ll be asked which chunks to stash.
9. Real-World Example
Scenario
We’re editing the login page, but a teammate asks us to fix a bug on another branch.
Steps
- Save our current work:
git stash push -m "WIP: login page UI" - Switch to the bug fix branch:
git checkout bugfix/user-profile - Fix the bug, commit, and return to our original branch:
git checkout main - Restore our stashed login page work:
git stash pop
Done! We didn’t lose anything.
10. Summary Table
| Task | Command |
|---|---|
| Stash tracked changes | git stash |
| Stash tracked + untracked | git stash -u |
| Stash with message | git stash push -m "message" |
| View stashes | git stash list |
| Apply stash | git stash apply |
| Apply + delete stash | git stash pop |
| Delete specific stash | git stash drop |
| Clear all stashes | git stash clear |
| Stash specific files | git stash push file1 |
Final Thoughts
Git stash is one of the most useful tools for real-world development.
It helps us stay organized, avoid messy commits, and switch tasks without stress.