Interview Q&A / Git & GitHub

BeginnerIn-depth3 min readUpdated 2 Oct 2026

What is git stash, and when would you use it?

SHORT ANSWER

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

bash
git status --short
text
 M src/products/Filter.jsx
 M src/products/api.js

Save it with a message:

bash
git stash push -m "price filter, half done"
text
Saved working directory and index state On feature/price-filter: price filter, half done

Now you are clean. Fix the bug:

bash
git switch main
git switch -c fix/checkout-total
# fix, commit, push, open PR

Come back and restore your work:

bash
git switch feature/price-filter
git stash list
text
stash@{0}: On feature/price-filter: price filter, half done
bash
git stash pop

Your 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:

CommandWhat it does
git stashStash tracked changes (staged and unstaged)
git stash push -m "msg"Stash with a description
git stash -uAlso stash untracked (new) files
git stash listShow all stashes
git stash show -p stash@{1}Show the diff of a stash
git stash popApply 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 clearDelete 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 pull when local edits are blocking it.
  • Realising you started coding on main by 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 stash ignores 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 clear without 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 pop and git 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.

#git#stash#workflow

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.