Chapter 9: Git Stash
Git stash. ☕
It’s February 13, 2026, still Hyderabad, and imagine this situation (it happens to everyone):
You’re working on a feature, your code is half-broken and messy, you have 5 modified files + 2 new files… Suddenly your team lead messages: “Hey, can you quickly fix that bug in production on the main branch? It’s urgent!”
What do you do? You don’t want to commit half-done messy code. You don’t want to create a temporary branch just for one small fix. You don’t want to lose your current work either.
This is exactly what git stash is made for.
What is git stash? (Simple human explanation)
git stash is Git’s “save my current work temporarily and clean my working directory” command.
It does three things at once:
- Takes all your uncommitted changes (both staged and unstaged)
- Saves them in a safe place (a special stack inside .git/)
- Reverts your working directory and index to match the last commit (clean slate)
Later, when you’re ready, you can bring those changes back exactly as they were — even across branches.
Think of it like:
- You’re cooking a complicated biryani → half-done, masala everywhere, rice soaking
- Someone says “quickly make tea for guests”
- You stash the biryani mess in the fridge (nothing spoils)
- Kitchen becomes clean → you make tea quickly
- After guests leave → you take biryani out of fridge and continue exactly where you left off
That’s git stash.
The Stash Stack – Very Important Concept
Git keeps stashes in a stack (like a pile of plates):
- Newest stash = stash@{0}
- Previous one = stash@{1}
- Older ones = stash@{2}, etc.
You can:
- Apply the latest stash (git stash apply or git stash pop)
- Apply an older one
- List all stashes
- Drop (delete) stashes you don’t need
Hands-on Example – Let’s Do It Step by Step
Create a small playground:
|
0 1 2 3 4 5 6 7 8 |
mkdir git-stash-demo cd git-stash-demo git init |
Make initial commit:
|
0 1 2 3 4 5 6 7 8 |
echo "# Todo App" > README.md git add README.md git commit -m "Initial commit" |
Now start real work:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Edit existing file echo "- Learn Git stash" >> README.md # Create new file echo "body { background: #f0f0f0; }" > style.css # Stage something echo "<h1>Todo</h1>" > index.html git add index.html |
Check status:
|
0 1 2 3 4 5 6 |
git status |
You should see:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: index.html Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md Untracked files: (use "git add <file>..." to include in what will be committed) style.css |
→ Messy! Half staged, half not, new file, modified file.
Now — emergency! You need to quickly fix something on main, but you don’t want to commit this mess.
Stash everything:
|
0 1 2 3 4 5 6 |
git stash push -m "WIP: todo app styling and header" |
Or shorter (most people do this):
|
0 1 2 3 4 5 6 |
git stash |
You’ll see:
|
0 1 2 3 4 5 6 |
Saved working directory and index state WIP on main: abc1234 Initial commit |
Now check:
|
0 1 2 3 4 5 6 |
git status |
|
0 1 2 3 4 5 6 7 |
On branch main nothing to commit, working tree clean |
→ All your changes disappeared! (but they are safe)
Look at your stashes:
|
0 1 2 3 4 5 6 |
git stash list |
Typical output:
|
0 1 2 3 4 5 6 |
stash@{0}: WIP on main: abc1234 Initial commit |
Bring your work back (two common ways)
Way 1: Apply + keep the stash (safe)
|
0 1 2 3 4 5 6 7 |
git stash apply # or git stash apply stash@{0} ← explicit |
→ Changes come back, stash still exists in the list
Way 2: Apply + remove from stack (most common)
|
0 1 2 3 4 5 6 |
git stash pop |
→ Same as apply + then git stash drop
Now your changes are back exactly as they were — staged files still staged, unstaged still unstaged.
Useful & Common Stash Commands (2026 Daily Use)
| What you want to do | Command | Effect |
|---|---|---|
| Stash everything (recommended) | git stash push -m “descriptive message” | Saves with message — easier to remember later |
| Quick stash (no message) | git stash | Default message: “WIP on branch…” |
| Stash only unstaged changes (keep staged) | git stash –keep-index | Useful when you want to commit staged part first |
| Stash including untracked files | git stash push –include-untracked | or -u — very common! |
| Stash everything including ignored files | git stash push –all or -a | Rare — includes .env, logs, etc. |
| List all stashes | git stash list | See the stack |
| Show what’s inside a stash | git stash show git stash show -p | -p = full diff |
| Apply latest stash (keep in stack) | git stash apply | Safe — can apply multiple times |
| Apply specific stash | git stash apply stash@{2} | Useful when you have many stashes |
| Apply + delete from stack | git stash pop | Most common daily flow |
| Delete a stash | git stash drop stash@{1} | or git stash drop → drops latest |
| Delete all stashes | git stash clear | Careful — irreversible |
Real-World Scenarios Where git stash Saves You
- Quick bug fix on main → stash → checkout main → fix → commit → push → stash pop → continue feature
- Switch branch suddenly → git stash → git checkout feature/login → work → later git stash pop
- Pull fails because of local changes → git stash → git pull → git stash pop
- Want to test clean state → git stash → test → git stash pop
Common Mistakes & Gotchas
- Forgot to stash untracked files → they stay behind → git stash -u fixes this
- Pop on wrong branch → changes might conflict → always check git status after pop
- Too many unnamed stashes → hard to know which is which → always use -m “message”
- Trying to stash when nothing to stash → Git will tell you “No local changes to save”
Mini Homework – Try Right Now!
- Make some messy changes in any repo (edit 2 files, create 1 new file, stage 1 file)
- git stash push -m “half-done dark mode changes”
- git status → should be clean
- Do something else (like git log, create a new file and commit)
- git stash pop
- See your changes come back perfectly
Got the feeling? git stash is your emergency bookmark for unfinished work.
Next?
- Want to see git stash –include-untracked vs normal?
- How to manage many stashes?
- Or move to conflicts when popping stashes?
Just tell me — we’ll keep going step by step. You’re doing really well! 🚀
