What is git stash and when should you use it?
Learn what git stash does, when to use it to shelve uncommitted changes, and how pop, apply, and -u work, with examples and interview tips.
Expected Interview Answer
git stash temporarily shelves uncommitted changes (both staged and unstaged) so your working directory becomes clean, letting you switch context without committing half-finished work.
The stashed changes are saved onto a stack you can inspect with git stash list and restore later with git stash pop or git stash apply. It is ideal when you need to pull, switch branches, or handle an urgent fix but are not ready to commit. By default untracked and ignored files are left behind unless you add -u or -a.
- Keeps the working directory clean without a throwaway commit
- Lets you switch branches safely mid-task
- Maintains a stack of multiple stashes
- Can include untracked files with -u
- Reversible via pop or apply
AI Mentor Explanation
Rain interrupts an innings, so groundstaff roll the covers over the exact state of the pitch and the score is recorded precisely as it stands. Play pauses, the ground is cleared, and later everything resumes from that saved position. git stash covers your in-progress changes the same way, clearing the working area so you can attend to something else and roll back the covers when ready.
Step-by-Step Explanation
Step 1
Make some changes
Edit tracked files so your working directory has uncommitted modifications you are not ready to commit.
Step 2
Stash them
Run git stash push -m "message" to shelve staged and unstaged changes and clean the working tree.
Step 3
Do other work
Switch branches, pull updates, or fix an urgent bug with a clean working directory.
Step 4
Inspect the stack
Use git stash list to see saved stashes and git stash show -p stash@{0} to preview one.
Step 5
Restore your work
Run git stash pop to reapply the latest stash and remove it, or git stash apply to keep it on the stack.
What Interviewer Expects
- Knows stash shelves uncommitted work onto a stack
- Difference between pop and apply
- Knows untracked files need -u to be stashed
- Realistic use cases like switching branches or pulling
- Awareness of stash conflicts on reapply
Common Mistakes
- Thinking stash commits changes to history
- Assuming untracked files are stashed by default
- Confusing pop (removes) with apply (keeps)
- Forgetting to name stashes and losing track of them
- Believing stash is shared across clones or pushed to remote
Best Answer (HR Friendly)
“git stash is like a pause button for your unfinished code changes. It tucks your work away safely so you can do something else, then brings it back exactly as it was when you are ready.”
Code Example
# Shelve current changes with a label
git stash push -m "wip: login form"
# Include untracked files too
git stash push -u -m "wip: with new files"
# See what is stashed
git stash list
# Preview a stash's diff
git stash show -p stash@{0}
# Reapply the latest stash and drop it
git stash pop
# Or reapply but keep it on the stack
git stash apply stash@{0}Follow-up Questions
- What is the difference between git stash pop and git stash apply?
- How do you stash untracked or ignored files?
- How do you apply a stash to a different branch?
- How do you resolve conflicts when popping a stash?
- How can you create a branch directly from a stash?
MCQ Practice
1. By default, which files does git stash NOT save?
git stash saves staged and unstaged tracked changes but ignores untracked files unless you pass -u (or -a to also include ignored files).
2. Which command reapplies a stash and removes it from the stack?
git stash pop reapplies the most recent stash and deletes it, while git stash apply reapplies but leaves the stash on the stack.
3. What does git stash list display?
git stash list shows every stash currently saved on the stash stack, each with a reference like stash@{0}.
Flash Cards
What does git stash do? — Shelves uncommitted staged and unstaged changes onto a stack, leaving the working directory clean.
pop vs apply? — pop reapplies and removes the stash; apply reapplies but keeps it on the stack.
How to include untracked files? — Use git stash push -u (or -a to also include ignored files).
How to view saved stashes? — git stash list, then git stash show -p stash@{n} to inspect one.