Definition
What it is
git stash takes all modified tracked files and staged changes, saves them as a commit on a hidden stack (refs/stash), and resets the working tree to match HEAD. git stash pop applies the top stash and deletes it from the stack. git stash apply applies without deleting. git stash list shows the stack, each entry with a message (WIP on main: <hash> <subject>).
By default, stash ignores untracked files. git stash -u includes them; git stash -a also includes ignored files. Neither is the default because losing untracked work by accident is a common (and painful) mistake.
Why it matters
Git refuses to switch branches if uncommitted changes would conflict with the target. Stash solves that: shelve the changes, switch, do the urgent work, switch back, restore. Without stash, you would have to commit half-done work to the branch (noise in history), or copy-paste the changes manually (dangerous).
Stash vs commit vs worktree
Aspect | Stash | Commit | Worktree |
|---|---|---|---|
Persistence | Stack, local | Branch | Filesystem |
Visibility |
|
| Filesystem path |
Sharing | No | Yes | No |
Use case | Quick context switch | Permanent history | Long parallel work |
Risk | Forgetting to pop | Clutter if WIP | Disk space |
The stash stack
graph TD W[Working tree with changes] -->|stash push| S[Stash stack] S -->|stash pop| W2[Working tree restored] W2 -->|stash push| S2[Stash stack grows] S2 -->|stash drop| X[Dropped] style S fill:#E8F5E9 style X fill:#FFEBEE
How to use stash well
Stash without regret
Name your stashes
git stash push -m "wip: fix login"beatsWIP on main: <hash>. You will not remember which is which.Include untracked with `-u`
git stash -usaves new files too. Without it, a new file is left behind and lost on nextgit clean -fd.Prefer `pop` over `apply`
popdeletes from the stack;applydoes not.applyaccumulates duplicated stashes you never clean.Drop old stashes
git stash clearwipes all. Review withgit stash listfirst.Prefer worktrees for long work
If you switch contexts for hours, use
git worktree addinstead of a long-lived stash.
Common failures
Origin
The word comes from mid-19th-century American English: a stash is a hidden store, originally of money or contraband. The verb "to stash" means to hide something for later. Git adopted the word in 2007 for its feature that hides working-tree changes on a stack, precisely mirroring the physical metaphor: a hidden place where you put something until you need it.
git stash is used to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
— git-stash(1) manual, 2024
Evidence
FAQ
- What does stash exclude?
Untracked files by default. Use
-uto include them,-afor ignored files too.- Can I recover a dropped stash?
Yes, with
git fsck --no-reflog | awk '/dangling commit/ {print $3}'thengit stash apply <hash>, but only if gc has not run.- Stash vs commit?
Stash is temporary and local; commit is permanent and shareable. Use stash for context switches, commit for history.
- Stash vs worktree?
Stash shelves changes; worktree gives a second working directory. Use worktrees for hours-long parallel work, stash for minutes-long switches.
Connections
Git project (2024). git-stash(1). https://git-scm.com/docs/git-stash
Chacon, S. & Straub, B. (2014). Pro Git, Chapter 7. https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning
Atlassian (2024). Git stash. https://www.atlassian.com/git/tutorials/saving-changes/git-stash
Etymology
Origin: borrowed
stash(noun): American English, mid-19th century, "a hidden store of money or supplies"
Semantic network
● broader ● narrower ● related
Sources
Git project (2024). git-stash(1). https://git-scm.com/docs/git-stash
Chacon, S. & Straub, B. (2014). Pro Git, Chapter 7. https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning
Atlassian (2024). Git stash. https://www.atlassian.com/git/tutorials/saving-changes/git-stash