How does git stash help in daily work?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
stash shelves uncommitted changes (tracked files by default), resetting the tree clean so you can switch context; git stash pop restores them later. Stacks multiple entries, supports messages, -u includes untracked files.
Detailed explanation
Context switching with dirty work causes either broken builds or commit-litter ('WIP'). stash snapshots modified tracked files plus index state onto a stack, restoring a clean tree instantly. Common flow: stash → switch branch/pull/hotfix → return → pop (applies + removes) or apply (keeps entry). Nuances worth knowing: untracked/new files need -u flag; stashes survive across branches (they're not branch-bound); popping can conflict if the base shifted — resolve like merges; drop clears entries you no longer need. Alternatives deserve mention: dedicated WIP branches or commit-amend flows scale better for long-lived shelving, and worktrees run multiple checkouts simultaneously without stashing at all. Stash shines for short interruptions — urgent review requests, quick hotfixes, pulling when local edits block merge.
Example
git stash push -m "wip: cart redesign" -u # include untracked
git checkout main && git pull # handle urgent update
git checkout feature/cart
git stash list
git stash pop # restore + remove entry
# conflict while popping? resolve, then:
git stash dropInterview tip
Give the interrupt-driven scenario (hotfix mid-feature) rather than defining abstractly. The -u untracked-files gotcha shows hands-on usage.
Common mistakes
- Forgetting untracked files don't stash by default.
- Assuming stashes belong to specific branches.