Have you ever been in the middle of a complex feature, writing messy code, when suddenly a colleague asks you to fix a critical bug on production?
You can’t commit your broken code. You can’t lose your work. So, what do you do?
You stash it.
Introduction
git stash is one of those commands that feels like magic once you start using it. It takes your uncommitted changes (both staged and unstaged), saves them away for later use, and reverts your working directory to the last clean commit.
Think of it as a “Cut and Paste” for your entire project. You cut your current work, go do something else, and paste it back when you’re ready.
Git Stash Command Reference
| Command | What It Does |
|---|---|
git stash |
Stash all tracked changes (staged + unstaged). |
git stash -u |
Stash tracked and untracked (new) files. |
git stash pop |
Apply latest stash and remove it from the list. |
git stash apply |
Apply latest stash but keep it in the list. |
git stash list |
View all stashed entries with timestamps. |
git stash pop stash@{1} |
Apply a specific entry by index. |
git stash drop stash@{0} |
Delete a specific stash entry. |
git stash clear |
Delete all stash entries permanently. |
How to Stash Your Work
Using stash is incredibly simple. When you have modified files:
git stash
That’s it. Your directory is now clean. You can switch branches, pull updates, or fix that bug.
Getting Your Work Back
When you’re ready to resume your work, you have two main options:
- Pop: Applies the changes and removes them from the stash list.
git stash pop - Apply: Applies the changes but keeps a copy in the stash (useful if you want to apply it to multiple branches).
git stash apply
I use pop 99% of the time because I like keeping my stash list clean.
Managing Multiple Stashes
You can stash more than once! Git keeps a stack of them. To see what you’ve stored:
git stash list
You’ll see output like this:
stash@{0}: WIP on feature-login: 1234567 Fixed header
stash@{1}: WIP on bug-fix: 89abcde Removed console logs
If you want to apply a specific one (not just the latest), you can refer to it by index:
git stash pop stash@{1}
Pro Tip: Stashing Untracked Files
By default, git stash only saves tracked files (files that Git already knows about). If you created a brand new file, it might be left behind.
To stash everything, including new files, use the -u (include untracked) flag:
git stash -u
Related tools you may want to try next.
B12.io Recently, I have found an AI-powered platform that enables you to create professional websites, pages, posts, and emails with ease. I will also give it a try and soon write a new post about B12.io (I am working on my coding post at the moment :).
Conclusion
git stash is a daily essential for any developer who multitasks. It keeps your commit history clean (no more “WIP” commits!) and gives you the flexibility to jump between contexts instantly.
Give it a try next time you’re interrupted!
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for you