What is git stash, and when would you use it?
git stash saves your uncommitted changes in a temporary stack and gives you a clean working directory, so you can switch branches or pull without committing half-done work. Later, git stash pop brings the changes back and removes them from the stack; git stash apply brings them back but keeps the stash.
Explain it simply
You are halfway through building a new filter on the products page. Your lead messages: "Production bug on the checkout page, please look now." Your code does not even compile yet, so you do not want to commit it. But Git will not let you switch branches if your changes would be overwritten.
git stash is like putting your unfinished work in a drawer. Your desk becomes clean, you handle the urgent task, and then you take the work back out of the drawer exactly as you left it.
Example
git status --short M src/products/Filter.jsx
M src/products/api.jsSave it with a message:
git stash push -m "price filter, half done"Saved working directory and index state On feature/price-filter: price filter, half doneNow you are clean. Fix the bug:
git switch main
git switch -c fix/checkout-total
# fix, commit, push, open PRCome back and restore your work:
git switch feature/price-filter
git stash liststash@{0}: On feature/price-filter: price filter, half donegit stash popYour two modified files are back, and the stash entry is removed.
How it works
Each stash is stored internally as a commit that is not on any branch. Stashes form a stack: the newest is stash@{0}, the one before it stash@{1}, and so on.
Useful commands:
| Command | What it does |
|---|---|
git stash | Stash tracked changes (staged and unstaged) |
git stash push -m "msg" | Stash with a description |
git stash -u | Also stash untracked (new) files |
git stash list | Show all stashes |
git stash show -p stash@{1} | Show the diff of a stash |
git stash pop | Apply the latest stash and remove it |
git stash apply stash@{1} | Apply a stash but keep it in the list |
git stash drop stash@{1} | Delete one stash |
git stash clear | Delete all stashes |
If pop hits a conflict, Git applies what it can, shows the conflict, and keeps the stash so you do not lose it. Resolve the conflict, then git stash drop.
Real-world use
- Switching to an urgent bug fix in the middle of a feature.
- Running
git pullwhen local edits are blocking it. - Realising you started coding on
mainby mistake: stash, create the right branch, pop. - Quickly testing whether a bug existed before your changes: stash, test, pop.
Common mistakes
- Forgetting that plain
git stashignores new untracked files. They stay in your folder. Use-u. - Piling up ten unnamed stashes and not knowing which is which. Always add
-m. - Using stash as long-term storage. If the work matters for more than a day, commit it on a branch instead.
- Running
git stash clearwithout checking the list first.
What to say in the interview
"git stash temporarily saves my uncommitted changes and resets the working directory to the last commit, so I can switch branches or pull without making a messy half-done commit. I use git stash push -m with a message, and -u if I have new files. When I come back, git stash pop restores the changes and removes the stash, while apply restores them but keeps the stash. I use it for short interruptions, like an urgent bug fix; for anything longer I commit to a branch."
Follow-up questions
- What is the difference between
git stash popandgit stash apply? - How do you stash untracked files?
- How would you move stashed changes onto a new branch?
- How do you recover a stash you dropped by mistake?
Go deeper
Read these once the basics above feel easy.
git stash branch new-branch-name stash@{0} creates a new branch from the commit where the stash was made, applies the stash there and drops it. This avoids conflicts when the original branch has moved on a lot. A dropped stash can sometimes be found with git fsck --unreachable | grep commit, then applied by its hash, but it is much easier to never drop stashes you have not checked.