Stash Deep Dive
apply safe hai pop se. -u se naye files bhi aayenge. Stash long-term storage nahi hai, important kaam branch mein commit karo!
git stash saves your uncommitted changes — both staged and unstaged — and cleans your working directory. Think of it as putting your half-eaten meal in a tiffin box so you can clean the table.
Why stash? You are in the middle of feature work, and suddenly you need to switch branches. You do not want to commit half-done work — that pollutes history. You do not want to lose your changes either. Stash is the answer.
Key stash commands:
git stash(orgit stash push) — saves modified and staged changes, resets working directory to clean stategit stash list— view all stashes (stash@{0},stash@{1}, etc.)git stash pop— apply latest stash AND remove it from the listgit stash apply— apply latest stash but KEEP it in the list (safer)
Stash is a STACK — LIFO order. stash@{0} is always the most recent stash. Every new stash pushes older ones down the stack.
# Stash current work
echo "dirty work" >> app.js
git stash push -m "WIP: feature work"
# Working directory is clean!
git status # nothing to commit
# View stashes
git stash list
# stash@{0}: On main: WIP: feature work
# Apply the stash (safer)
git stash apply stash@{0}
# Or pop (apply + drop)
git stash pop
git stash clear, those changes are gone forever. Stash is a safety net for quick context switches, not a replacement for commits.The difference between pop and apply is simple but critical: pop = apply + drop, apply = apply only.
git stash pop applies the stash and immediately removes it from the stash list. If the apply succeeds cleanly, the stash is gone. If it causes a conflict, the stash is not dropped — Git protects you, but the workflow is confusing.
git stash apply applies the stash but keeps it in the list. You can verify the result, test your code, and then manually drop the stash when you are confident everything works.
Best practice: Use apply for important work. Verify the result. Then git stash drop manually. This gives you a safety net.
If pop causes a conflict, the stash is NOT dropped — Git keeps it so you can try a different approach. But the workflow is confusing. Better to use apply and be explicit.
# pop = apply + drop (convenient but risky)
git stash pop
# apply = apply only (safe, verify first)
git stash apply stash@{0}
# Check if everything looks correct
git status
# If good, manually drop
git stash drop stash@{0}
# Apply a specific older stash
git stash apply stash@{2}
git stash apply your default. Only use pop when you are absolutely sure the apply will succeed (e.g., the stash was just created and the branch has not changed). For any critical work, apply + verify + drop is the professional workflow.By default, git stash only stashes tracked, modified files. Untracked files — new files that Git has never seen — are NOT stashed by default!
This catches many developers off guard. You stash, switch branches, and discover your new file is still sitting there on the wrong branch — or worse, lost.
Three levels of stash inclusiveness:
git stash— only tracked, modified files (default)git stash -u— include untracked files too (most common need)git stash --all— include untracked AND ignored files (node_modules, build artifacts, etc.)
Best practice: Always use git stash push -m "message" -u. The message identifies the stash, and -u ensures new files are included. Make this your default stash command.
# Create tracked + untracked files
echo "modified" >> app.js # tracked
echo "new file" > new.js # untracked
# Default stash — ONLY saves tracked changes
git stash
# new.js is still sitting there, not stashed!
# Stash EVERYTHING (including untracked)
git stash -u
# Now new.js is also stashed
# Stash ALL (including ignored like node_modules)
git stash --all
# Best practice
git stash push -m "WIP: auth feature" -u
-u is one of the most common stash mistakes. You stash confidently, switch branches, and your new file is still in the working directory — on the wrong branch! Or you clean up and the file is gone. Always use -u unless you explicitly want to leave new files behind.If a stash will definitely conflict with your current branch, git stash branch is your escape route.
git stash branch <branchname> does two things: creates a new branch at the stash's parent commit, and applies the stash. Because the new branch starts at the exact commit where the stash was created, the apply is conflict-free.
This is the cleanest way to recover a long-forgotten stash. Instead of fighting conflicts on your current branch, you create a dedicated recovery branch where the stash applies cleanly.
After the branch is created and stash applied, commit your work and merge it properly through a pull request or merge.
# Old stash that will conflict with current main
git stash list
# stash@{0}: On main: WIP: old feature from 3 weeks ago
# Instead of risky pop/apply, create a branch!
git stash branch stash-recovery stash@{0}
# This creates a new branch from the stash's parent
# and applies the stash automatically (no conflict!)
# Commit the stash work
git add -A
git commit -m "feat: recovered stash work"
# Now merge this branch to main properly
git checkout main
git merge stash-recovery
git stash branch for any stash older than a few days. The longer a stash sits, the more likely it is to conflict with your current branch. Creating a recovery branch is cleaner than resolving complex conflicts from stale stashes.Stash management is about discipline. A few well-maintained stashes are useful. A pile of 15 unnamed stashes is a nightmare.
Key management commands:
git stash show -p stash@{0}— preview what is in a stash before applyinggit stash drop stash@{1}— delete a specific stashgit stash clear— delete ALL stashes (dangerous, no confirmation!)
Rules for healthy stash usage:
- Always use messages:
git stash push -m "WIP: search feature" - Keep stash count under 5 — more than that is a code smell
- If a stash is older than 1 week, commit it to a branch
- Never use
git stash clearwithout checkinggit stash listfirst
Stash entries have no expiry — they stay until you drop them. This means forgotten stashes accumulate silently. Regular cleanup prevents this.
# Preview stash before applying
git stash show -p stash@{0}
# Delete specific stash
git stash drop stash@{1}
# Delete ALL stashes (be careful!)
git stash clear
# Stash management best practices:
# 1. Always use messages
git stash push -m "WIP: search feature"
# 2. Don't let stashes pile up (> 5 is a code smell)
git stash list
# If you have 10+ stashes, commit them to branches instead
# 3. If stash is older than 1 week, commit it
git stash branch old-work stash@{3}
Lo kar liya — Key Points:
- ✅
git stashsaves uncommitted changes and cleans the working directory for quick context switching - ✅
git stash applykeeps the stash (safer);git stash popremoves it after applying - ✅ Use
git stash -uto include untracked files — by default, new files are NOT stashed - ✅
git stash branch <name>creates a conflict-free branch from a stash for safe recovery - ✅ Always use
git stash push -m "message"— unlabeled stashes are impossible to identify later - ✅ Don't hoard stashes — if it's important, commit it to a branch; keep stash count under 5
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login