Mid-task, the request lands: switch to main, fix production, review someone's branch. Your working tree has half-finished changes that belong to neither. git stash parks those changes — cleanly, reversibly, without a fake commit — so the switch happens now and the work resumes later, intact.
The round trip #
$ git stash
Saved working directory and index state WIP on main: f3e2d1c
$ git status # clean — switch anywhere
$ git stash pop # back exactly as you left it
stash takes tracked, modified files and saves them to a stack; the working tree returns to HEAD. pop restores the newest stash and removes it from the stack. The pair covers the daily case — the interruptions note of branches.
The variants that matter #
| Command | Effect |
|---|---|
git stash | Park tracked changes |
git stash -u | Include untracked files too |
git stash -p | Choose hunks interactively |
git stash apply | Restore but keep the stash |
git stash list | The stack, newest first |
git stash pop stash@{2} | Restore a specific one |
git stash drop | Delete one entry |
-u is the flag people miss: a new file you just started is untracked, and plain stash leaves it sitting in the tree. apply versus pop is the "keep a copy" choice — apply when the stash is worth keeping (an experiment you may re-run), pop for the ordinary interruption.
Naming and finding #
Stashes are anonymous by default, which is how they become archaeology. Give the message at save time:
$ git stash push -m "toc scroll-spy, halfway"
$ git stash list
stash@{0}: On main: toc scroll-spy, halfway
$ git stash show -p stash@{0} # inspect without applying
Inspecting before applying matters when the stack is deep — show -p is diff against where the stash was made.
Beyond the interruption #
Two professional moves use stash as a primitive:
$ git stash
$ git switch -c feature/scroll-spy
$ git stash pop
# Diff against the stash without touching the tree
$ git diff stash@{0} -- src/toc.js
The first is the standard "this grew into a feature" exit; the second compares current work to the parked version.
Recovering a dropped stash runs through reflog — recoverable, but not the plan. Parallel checkouts that avoid stashing entirely are worktrees. The Git series continues.
