Stash: the pocket
Park half-done work, switch context, pick it back up.
1 Learn the idea
You are halfway through a change when someone says "quick, fix production!". Your work is not ready to commit, and git won't let you switch branches if that would overwrite your edits. git stash handles this.
git stash: save tracked changes (staged and unstaged) and clean the working tree.git stash -u: also include untracked (new) files, which a plain stash leaves behind.git stash -m "note": add a label so you remember what it was.git stash list: stashes form a stack.stash@{0}is the newest.git stash pop: reapply the newest stash and remove it.git stash applyreapplies but keeps it.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ edit menu.md "Soup, Salad, Tiramisu (half-written…)"
We are on
salad, midway through an edit. It is not ready to commit.$ git switch main
An urgent fix is needed on
main. The switch is refused:mainhas a differentmenu.md, and switching would overwrite our edit.$ git stash
The change goes into the stash and the working tree is clean again. The stash remembers which commit it was based on.
$ git stash list
Stashes form a stack.
stash@{0}is the newest one. Its default label says where it was made.$ git switch main
The working tree is clean, so switching is allowed now.
$ echo "Open 9-17" > hours.md $ git add hours.md $ git commit -m "Add opening hours"
We make the urgent fix and commit it on
main.$ git switch salad
Back on our feature branch.
$ git stash pop
popputs the half-written tiramisu back into the working directory and removes the entry from the stash.$ echo "idea: vegan menu" > ideas.txt $ git stash -u -m "tiramisu + ideas"
ideas.txtis a new, untracked file, and a plain stash would leave it behind.-uincludes untracked files.-mgives the stash a readable label.$ git stash list
The labelled stash waits in the pocket.
git stash popwould bring back both files.
git stash [-u] [-m "note"]Pocket uncommitted changesgit stash listSee what is in the pocketgit stash popReapply newest and remove itgit stash apply / dropReapply but keep it / throw one away3 Check your understanding
You created a brand new file draft.md (never added) and run plain git stash. What happens to draft.md?
What is the difference between git stash pop and git stash apply?
Look at the graph. What does the stash@{0} label mark?
stash@{0} WIP on main: e375f09 Add pricesYour changes will need a few days of on-and-off work. Best place to keep them?
4 Practice for real
Hotfix without losing your place
You are mid-edit on salad. Commit a new file hours.md on main (message must mention "hours"). Then return to salad and get your unfinished menu.md edit ("Soup, Salad, Dressing") back, with the stash empty.