06 · Stash¶
← Overview · Prev: Undo and recovery · Next: Branch management →
Stash is a temporary clipboard for uncommitted changes: set work aside, switch context, and come back without making a messy commit.
git stash push -u -m "wip: description" # -u also stashes untracked files
git stash list
git stash show -p stash@{0} # the diff a stash contains
git stash pop # apply the newest stash and drop it
git stash apply stash@{1} # apply but keep it in the list
git stash drop stash@{1} # delete one
git stash clear # delete all (careful)
git stash branch <new-branch> stash@{0} # new branch from the commit the stash was taken on
stash@{0} is always the newest; stash@{1} the one before, and so on.
Useful variants:
git stash push -- <path>— stash only some files.git stash push -p— choose hunks interactively.git stash push --keep-index— stash only unstaged changes.-a/--allalso stashes ignored files.
Where stashes live¶
In the local .git (refs/stash). They are not pushed, and other clones cannot see them.
On conflict, pop/apply leaves both versions in the file for you to resolve; a conflicted pop does not drop the stash.
Stash vs commit¶
Use stash to:
* switch branches quickly when work isn't ready,
* test something on a clean working tree,
* create a safety net before a destructive command (git-adm discard does this).
Use a WIP commit (amend or squash later) when:
* you are about to rebase (stashes survive but can conflict),
* the work will sit for more than a day or two,
* you want it visible in git log and safe on a pushed branch.
git-adm: git-adm stash list|save|show|pop|apply|drop — pickers by number, timestamped messages
(<branch> - manual stash YYYY-MM-DD-HH.MM).