30 Mins

8. Git Stash — Saving Work in Progress

Workflow

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.

Bash
git stash

Switch to the urgent fix, apply it, commit, and push:

Bash
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:

Bash
git checkout feature/dashboard
git stash pop

Managing Multiple Stashes

Stash with a descriptive message:

Bash
git stash push -m "WIP: half-finished search autocomplete"

See all stashes:

Bash
git stash list

Apply a specific stash (without removing it from the list):

Bash
git stash apply stash@{1}

Apply the most recent stash and remove it from the list:

Bash
git stash pop

Delete a stash you no longer need, or delete ALL stashes:

Bash
git stash drop stash@{0}
git stash clear

Knowledge Check

Ready to test your understanding of 8. Git Stash — Saving Work in Progress?

Back