Lessons · Git · stash: putting work aside
Shelving work for a moment
git stash puts your uncommitted changes on a local stack and leaves a clean working tree. git stash pop brings the top one back and drops it from the stack.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
You are mid-change and must switch branches right now: a hotfix, a review, a colleague's question. Stash, switch, do the thing, switch back, pop.
How to think about it
Stash with a message (push -m 'why') so the list stays readable. pop brings it back and removes it; apply brings it back and keeps it. A stash is local and never pushed.
Worked example
echo wip >> a.txtMid-change, and you need a clean tree now.
git stashShelves the change; the working tree is clean.
git status --shortNothing: safe to switch branches.
git stash liststash@{0}: WIP on main: the stack, newest first.
git stash popRe-applies the change and drops it from the stack.
Your turn
Bring the shelved work back and drop it from the stack.
git stash
Try it in a real repository
The trap
Stashes are local and easy to forget. A month later the list has five entries and nobody knows what they were: stash with a message, git stash push -m 'why'.