When working with Git, sometimes you need to switch contexts quickly — maybe to review someone else’s code or fix a critical bug — but you’re in the middle of unfinished work that you don’t want to commit yet. This is where Git stash comes in handy.
Git stash lets you temporarily save your changes without committing them, so you can come back later and resume exactly where you left off.
What Is Git Stash?
Git stash is a command that stores your uncommitted changes (both staged and unstaged) in a safe place and reverts your working directory back to the last commit. Think of it like putting your current work on a temporary shelf.
When to Use Git Stash?
- You need to switch branches but aren’t ready to commit.
- You want to pull or merge the latest code but have local changes.
- You’re experimenting and want to save progress without cluttering your commit history.
Basic Git Stash Commands
1. Stash Your Changes
git stash
This command saves your local modifications and reverts the working directory to match the HEAD commit (latest commit in your current branch).
2. View Your Stashes
git stash list
See all your stashed changes stored in the stash stack.
3. Apply Your Stash
To reapply your stashed changes back to your working directory without removing them from the stash list:
git stash apply
You can specify a stash if you have multiple, for example:
git stash apply stash@{2}
4. Pop Your Stash
This reapplies the changes and removes the stash from the list:
git stash pop
5. Drop a Stash
If you want to remove a stash without applying it:
git stash drop stash@{0}
6. Clear All Stashes
git stash clear
Advanced Git Stash Options
- Stash Untracked Files:
By default, untracked files (new files not yet added) are not stashed. To stash them too:git stash -u
- Stash with a Message:
Adding a description helps identify stashes later:git stash save "WIP: fixing header layout"
Example Workflow Using Git Stash
- You’re halfway through adding a new feature but suddenly need to fix a bug in master.
- Run
git stash
to save your current work. - Switch to the master branch and fix the bug.
- Commit and push your fix.
- Return to your feature branch.
- Use
git stash pop
to restore your unfinished work and continue.
Final Thoughts
Git stash is a powerful tool that keeps your workflow flexible and your commits clean. It’s a lifesaver when juggling multiple tasks or needing quick context switches without losing work.