Stashing Work in Progress
git stash
Scenario: You're halfway through an edit when you need to switch branches - but Git won't let you because of uncommitted changes, and you're not ready to commit. git stash is the answer.
stash tucks your uncommitted changes away on a shelf and gives you a clean working directory. Later, you pop them back.
| Command | Does |
|---|---|
git stash | Shelve current changes |
git stash list | Show stashes |
git stash pop | Re-apply the latest stash and remove it |
git stash apply | Re-apply but keep it in the list |
git stash drop | Delete a stash |
$ git stash
Saved working directory and index state WIP on main: a1b2c3d Add readme
$ git switch hotfix # clean tree, switch freely
# ...fix the bug, come back...
$ git switch main && git stash pop
Tip:git stash pop= apply + remove.git stash apply= apply but keep a copy (useful if you want the same changes on multiple branches).