8. Git Stash — Saving Work in Progress
You are halfway through a feature when your manager asks you to drop everything and fix an urgent bug in production. You can't commit half-finished code. You don't want to delete your changes. Git stash is a temporary shelf — it saves your incomplete work so you can switch contexts cleanly and come back later.
Stashing and Restoring
Save all current uncommitted changes to the stash. Your working directory is now clean.
git stash
Switch to the urgent fix, apply it, commit, and push:
git checkout main git checkout -b hotfix/payment-crash # ... make the fix ... git add . && git commit -m "hotfix: fix null crash in payment processor" git checkout main git merge hotfix/payment-crash git push
Return to your feature branch and restore your stashed work. Your half-finished code is back exactly as you left it:
git checkout feature/dashboard git stash pop
Managing Multiple Stashes
Stash with a descriptive message:
git stash push -m "WIP: half-finished search autocomplete"
See all stashes:
git stash list
Apply a specific stash (without removing it from the list):
git stash apply stash@{1}
Apply the most recent stash and remove it from the list:
git stash pop
Delete a stash you no longer need, or delete ALL stashes:
git stash drop stash@{0} git stash clear
Knowledge Check
Ready to test your understanding of 8. Git Stash — Saving Work in Progress?