Ever been midway through a feature and suddenly need to fix a bug or pull updates? That’s where Git stash comes in. It lets you temporarily save uncommitted changes without creating a commit, giving you a clean working directory to switch tasks—without losing progress.
How It Works
Git stash saves your current changes (staged or unstaged) on a stack, resets your working directory to the last commit, and lets you reapply those changes later. Stashes live locally—they aren’t pushed to GitHub.
Quick Commands
git stash→ stash tracked filesgit stash -u→ include untracked filesgit stash push -m "message"→ stash with a notegit stash list→ see all stashesgit stash apply→ reapply latest stashgit stash pop→ reapply and remove stash
You can also create a new branch from a stash using git stash branch <name> —super handy for experimenting.
Example Workflow
- Save your half-done feature:
git stash push -m "Half-done login feature"
Switch to main, fix a bug, commit your changes.
Return to your feature branch:
git stash pop
Conflicts? Resolve them like any merge conflict.
Tips for Beginners
Always add a message with
-mto remember what’s in each stash.Use
-uto include new files you haven’t tracked yet.Clean up old stashes with
git stash droporgit stash clear.For longer work, consider creating a temporary branch instead.
Git stash keeps your workflow flexible and clean, letting you handle interruptions without messy “WIP” commits. Master it, and you’ll switch tasks like a pro.
Top comments (0)