Git stash is your safety net
·cheatly.dev
You're mid-feature, files half-changed, and someone asks you to look at a bug on another branch. You can't switch branches with uncommitted changes. You don't want to commit half-finished work. This is exactly what git stash is for.
The basic workflow
Stash Essentials
git stash
Save all uncommitted changes and clean the working treegit stash pop
Restore the most recent stash and remove it from the listgit stash list
See all stashed changesgit stash apply
Restore the most recent stash but keep it in the listgit stash drop
Delete the most recent stashThe typical flow:
git stash— your changes disappear, working tree is clean- Switch branches, fix the bug, commit, switch back
git stash pop— your changes are back exactly where you left them
Stashing specific things
Targeted Stashing
git stash -u
Include untracked (new) filesgit stash -m "message"
Stash with a description so you remember what it wasgit stash push path/to/file
Stash only specific filesAlways use
git stash -u if you have new files. Without -u, untracked files get left behind — you stash your changes but the new files stay in your working directory, which is almost never what you want.Managing multiple stashes
Stashes stack up. The most recent is stash@{0}, the one before is stash@{1}, etc.
Working with the Stack
git stash list
See all stashes with their index and messagegit stash show stash@{1}
See what changed in a specific stashgit stash pop stash@{2}
Restore a specific stash (not just the latest)git stash clear
Delete all stashes (careful!)If you find yourself with more than 2-3 stashes, you're probably better off committing to a temporary branch instead. Stashes are great for quick context switches, not long-term storage.
Stash in lazygit
If you use lazygit, stashing is even faster: press 5 to open the Stash panel, Space to apply, g to pop, d to drop. Visual and immediate.
For the full reference, see our git cheatsheet and lazygit cheatsheet.