Git stash = Temporarily save your UNCOMMITTED modifications.
Analogy: It is like putting your things in a secret drawer before doing something else.
Why?
git stash → switch → fix → come back → git stash popmkdir demo-stash && cd demo-stash
git init
echo "print('v1')" > app.py
git add . && git commit -m "Initial version"
# You start coding
echo "print('work in progress')" >> app.py
# EMERGENCY! You have to switch branch
git stash # Save the WIP
git status # Clean working directory!
# Do something else...
git checkout -b hotfix
echo "print('CRITICAL FIX')" > fix.py
git add . && git commit -m "Urgent fix"
# Go back to the original work
git checkout main
git stash pop # Recover the work in progressMagic! Your modifications are back.
| Command | Action |
|---|---|
git stash | Save the modifications |
git stash pop | Recover + delete from the stash |
git stash list | See all stashes |
git stash apply | Recover WITHOUT deleting |
git stash drop | Delete a stash |
git stash clear | Empty all stashes |
Classic situation:
git stashgit stash popStash = Your best friend for interruptions!