What is git stash and when to use it?
git stash temporarily shelves (stashes) changes you've made to your working copy so you can work on something else, then come back and re-apply them later.
Common Use Cases
1. Switching Branches Mid-Work
```bash
git stash git checkout main
git checkout feature git stash pop # Restore your work ```
2. Before Pulling Updates
```bash
git stash git pull origin main git stash pop ```
Basic Commands
```bash
git stash
git stash save "WIP: feature implementation"
git stash list
git stash pop
git stash apply
git stash apply stash@{1}
git stash drop stash@{0}
git stash clear ```
Stash Options
```bash
git stash -u
git stash -a
git stash --staged
git stash branch new-feature stash@{0} ```
Real Scenario
```bash
$ git status Changes not staged: modified: src/feature.js modified: src/component.js
$ git stash save "WIP: user authentication"
$ git checkout main $ git checkout -b hotfix/critical-bug
$ git commit -m "Fix critical bug" $ git push
$ git checkout feature $ git stash pop
```
Interview Q&A
Q: Difference between stash pop and apply?
A: pop applies stash and removes it from list. apply keeps stash in list for reuse.
Q: Can you stash untracked files?
A: Yes, use git stash -u (untracked) or git stash -a (all including ignored).
Q: What happens to stash after merge conflict?
A: Stash remains in list. Fix conflicts, then git stash drop to remove it.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.