Git Stash
Purpose
git stash temporarily shelves (stashes) uncommitted changes so you can switch context — e.g., check out a different branch to fix a bug — without committing half-finished work.
Architecture
The stash is a stack of WIP (work-in-progress) entries stored in the ref refs/stash. Each entry records:
- A commit for the index state
- A commit for the working tree state
- An optional commit for untracked files (with
--include-untracked)
Popping restores the saved state and removes the entry from the stack; applying restores without removing.
Implementation Notes
Save and restore:
git stash # stash tracked modified files
git stash -u # also stash untracked files
git stash -m "message" # stash with descriptive name
git stash pop # apply most recent stash and remove it
git stash apply # apply most recent stash, keep it in stack
git stash apply stash@{2} # apply a specific stash entryInspect the stash stack:
git stash list # list all stash entries
git stash show # show summary of most recent stash
git stash show -p # show diff of most recent stashClean up:
git stash drop # remove most recent stash entry
git stash drop stash@{2} # remove specific entry
git stash clear # remove all stash entriesCreate a branch from a stash (useful if context diverged):
git stash branch <branch-name> # create branch at stash point, apply stash, popTrade-offs
- Stashes are local — they are not pushed to remotes.
- Stash conflicts are possible when
pop/applyis called on a working tree that has diverged from when the stash was created. Resolve them like merge conflicts. - For longer-lived WIP, consider committing with
git commit --no-verify -m "WIP: ..."and amending later — commits are more durable and visible than stash entries. git stash clearis irreversible (stash entries are eventually garbage-collected).