Hone

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.txt
Mid-change, and you need a clean tree now.
git stash
Shelves the change; the working tree is clean.
git status --short
Nothing: safe to switch branches.
git stash list
stash@{0}: WIP on main: the stack, newest first.
git stash pop
Re-applies the change and drops it from the stack.

Your turn

Bring the shelved work back and drop it from the stack.

git stash 

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'.

Practise stash: putting work aside on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.