git stash — Saving Work Temporarily
The git stash command saves your uncommitted changes and restores a clean working directory. It's like putting your work on a shelf to deal with later.
When to Use Stash
- You need to switch branches but have uncommitted work
- You want to pull updates but have local changes
- You want to quickly test something on a clean state
Basic Commands
git stash— Save changes and clean working directorygit stash pop— Restore the most recent stash and remove itgit stash list— Show all stashed entriesgit stash drop— Remove the most recent stash without applying
How It Works
Working Dir (dirty) → git stash → Working Dir (clean)
↓
Stash Stack
[stash@{0}]
[stash@{1}]
[stash@{2}]
Stashes are stored in a stack — last in, first out.
Try it!
Practice stashing and restoring:
- Create and commit a file:
echo "base" > work.txt && git add work.txt && git commit -m "Add work" - Make changes:
echo "in progress" > work.txt - Stash the changes:
git stash - Verify clean state:
cat work.txt— shows "base" again - Restore the stash:
git stash pop - Verify restored:
cat work.txt— shows "in progress" again
Multiple Stashes
You can stash multiple times. Each new stash pushes onto the stack:
git stash # saves current work as stash@{0}
git stash # saves more work as stash@{0}, previous becomes stash@{1}
git stash list # shows all entries
git stash pop # restores stash@{0}