Ever been in the middle of a feature when you suddenly need to switch branches?
git stash is your superhero - it temporarily stores your unfinished changes
so you can work on something else without losing anything.
The Problem
You're deep into coding a new feature when suddenly:
- A critical bug needs immediate attention
- Your teammate needs help on their branch
- You need to pull the latest changes from main
But your current work isn't ready to commit. What do you do?
The Solution: git stash
Git stash saves your work-in-progress and gives you a clean working directory - without making a messy commit.
Save Your Work
Command
git stash
This saves all your uncommitted changes and cleans your working directory. Now you can safely switch branches!
Get Your Work Back
Command
git stash pop
This retrieves your saved changes and applies them back to your working directory. It also removes the stash from the list.
View All Your Stashes
Command
git stash list
See all your saved stashes with their labels. Useful when you have multiple stashes.
Apply a Specific Stash
Command
git stash apply stash@{1}
Apply a specific stash without removing it from the list.
Replace 1 with the stash number you want.
Clean Up Old Stashes
Command
git stash drop stash@{0}
Remove a stash you no longer need. Keep your stash list clean!
Why Git Stash is a Life Saver
A small Git feature that saves you hours of stress and keeps your commits clean.
Benefits of Using Stash
- Clean commits: No more "WIP" or "temp" commits cluttering your history
- Easy context switching: Jump between tasks without losing work
- Branch flexibility: Switch branches freely, even with uncommitted changes
- Reduced stress: Unexpected interruptions? No problem!
Pro Tips
- Use
git stash save "description"to add a message to your stash - Use
git stash -uto also stash untracked files - Use
git stash branch new-branchto create a branch from a stash
Conclusion
git stash is one of those small features that makes a huge difference in your daily workflow.
Next time you need to switch contexts unexpectedly, you'll be glad you know it!
Happy coding! May your stashes always pop cleanly.