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.
| Command | Does |
|---|---|
git stash | Shelve current changes (tracked, modified files) |
git stash push -m "note" | Shelve with a memorable label |
git stash list | Show all stashed entries |
git stash pop | Re-apply the latest stash and remove it from the list |
git stash apply | Re-apply but keep it in the list |
git stash drop | Delete 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 ingit statusorgit logonce it's shelved. If you stash, switch branches several times, and keep working,git stash listweeks later might turn up an entry you completely forgot existed. Get in the habit of checkinggit stash listoccasionally, and clearing it out withpopordroponce you're done with an entry - and note that a stash can itself conflict onpopif the file has since changed underneath it, in which case Git treats it much like a merge conflict.