Stashing Work in Progress

git stash

Scenario: You're halfway through an edit - files modified, nothing committed - when you suddenly need to switch branches. Git often blocks the switch because your uncommitted changes might get overwritten, and you're not ready to commit half-finished work. git stash is the escape hatch.

stash tucks all your current uncommitted changes away on a shelf and hands you back a clean working directory, as if you'd never touched anything. Later, you can pop them back out exactly as they were.

CommandDoes
git stashShelve current changes (tracked, modified files)
git stash push -m "note"Shelve with a memorable label
git stash listShow all stashed entries
git stash popRe-apply the latest stash and remove it from the list
git stash applyRe-apply but keep it in the list
git stash dropDelete a stash without applying it
$ git stash
Saved working directory and index state WIP on main: a1b2c3d Add readme
$ git switch hotfix     # clean tree, switch freely
# ...fix the bug, commit, come back...
$ git switch main && git stash pop
On branch main
Changes not staged for commit:
        modified:   app.py
Dropped refs/stash@{0}
Tip: git stash pop = apply the stash and remove it from the shelf. git stash apply = apply it but leave a copy behind - useful if you want to bring the same set of changes onto more than one branch.
Warning: A stash is easy to forget about - it isn't visible in git status or git log once it's shelved. If you stash, switch branches several times, and keep working, git stash list weeks later might turn up an entry you completely forgot existed. Get in the habit of checking git stash list occasionally, and clearing it out with pop or drop once you're done with an entry - and note that a stash can itself conflict on pop if the file has since changed underneath it, in which case Git treats it much like a merge conflict.