What is Git Stash?
Learn what git stash does, how it shelves staged and unstaged changes, and how to list, apply, and pop stashes with real command-line examples included.
Expected Interview Answer
`git stash` temporarily shelves your uncommitted changes, both staged and unstaged, restoring a clean working directory so you can switch context and reapply that work later exactly as it was.
Internally, a stash creates special commit objects that store the state of your working tree and index at the time you ran the command, and pushes a reference onto a stack (`refs/stash`) rather than writing history you'd see with `git log`. This is useful when you're mid-feature and need to urgently switch branches, pull upstream changes, or fix a bug without committing half-finished work. You can stash multiple times, list them with `git stash list`, inspect one with `git stash show -p`, and bring changes back with `git stash pop` (apply and remove) or `git stash apply` (apply and keep). Because stashes live outside normal branch history, they're easy to forget, so treat them as a short-term shelf, not a substitute for committing or branching.
- Lets you switch branches instantly without committing unfinished work
- Keeps both staged and unstaged changes together in one shelf
- Supports multiple named stashes for juggling several interruptions
- Non-destructive by default when using `apply` instead of `pop`
- Faster than creating throwaway commits just to clear the working tree
AI Mentor Explanation
Git stash is like a batter putting a half-finished shot selection on hold when rain stops play, tucking the pads and gloves aside to rush off the field. When play resumes later, everything is retrieved exactly as it was left, letting the batter pick up mid-innings without losing the earlier setup.
Step-by-Step Explanation
Step 1
Shelve current changes
`git stash` (or `git stash push -m "message"`) saves both staged and unstaged changes into a stash commit and resets the working directory to match HEAD.
Step 2
Switch context freely
With a clean working tree, you can switch branches, pull updates, or fix an urgent bug without your in-progress work getting in the way.
Step 3
List saved stashes
`git stash list` shows every stashed entry (`stash@{0}`, `stash@{1}`, ...) so you can track multiple interruptions at once.
Step 4
Preview a stash
`git stash show -p stash@{0}` displays the diff contained in a stash before you decide whether to bring it back.
Step 5
Reapply the work
`git stash pop` reapplies the most recent stash and removes it from the stack; `git stash apply` reapplies it but keeps it stashed in case you need it again.
What Interviewer Expects
- Explains that stash shelves uncommitted changes without creating a normal commit
- Knows stash captures both staged and unstaged changes
- Understands the difference between `pop` and `apply`
- Mentions the stash stack and `git stash list`
- Can describe a real scenario where stash is useful, like an urgent hotfix
Common Mistakes
- Confusing `git stash pop` (removes the stash) with `git stash apply` (keeps it)
- Forgetting stashed changes exist and losing track of them over time
- Not knowing stash can produce conflicts just like a merge if the tree has diverged
- Thinking stash is a substitute for committing work-in-progress properly
- Assuming stash is visible in normal `git log` output
Best Answer (HR Friendly)
“Git stash lets a developer temporarily set aside unfinished work to switch tasks quickly, then bring that exact work back later without losing anything. It's like putting a project on a shelf mid-way through so you can handle something urgent, then picking it right back up.”
Code Example
$ git status
Changes not staged for commit:
modified: src/utils/parser.ts
$ git stash push -m "WIP parser refactor"
Saved working directory and index state On main: WIP parser refactor
$ git switch hotfix/urgent-bug
# ...fix and commit the urgent bug...
$ git switch main
$ git stash list
stash@{0}: On main: WIP parser refactor
$ git stash pop
Dropped stash@{0}Follow-up Questions
- What is the difference between `git stash pop` and `git stash apply`?
- Can `git stash` include untracked or ignored files?
- How do you resolve a conflict that occurs when popping a stash?
- Can you stash only part of your changes?
- How does `git stash branch` work?
MCQ Practice
1. What does `git stash` primarily do?
`git stash` saves your uncommitted work aside and restores a clean working directory, without creating a regular commit in your branch history.
2. What is the difference between `git stash pop` and `git stash apply`?
`git stash pop` applies the changes and drops the stash from the stack, while `git stash apply` applies them but leaves the stash entry intact for reuse.
3. By default, what does `git stash` capture?
By default `git stash` captures modifications to tracked files, both staged and unstaged; untracked files need the `-u` flag to be included.
Flash Cards
What does `git stash` do? — Temporarily shelves uncommitted changes and restores a clean working directory.
`git stash pop` vs `git stash apply`? — `pop` applies and removes the stash entry; `apply` applies but keeps it on the stack.
How do you see all saved stashes? — `git stash list` shows every entry as `stash@{n}`, newest first.
How do you include untracked files in a stash? — Use `git stash push -u` (or `--include-untracked`).