Git Stash: A Developer’s Temporary Shelf
When working with Git, sometimes you’re in the middle of making changes
but suddenly need to switch branches or work on something else without
committing your unfinished work.
This is where Git Stash comes in handy.
What is Git Stash?
git stash
temporarily saves (or “stashes”) your uncommitted changes in
a hidden area, allowing you to switch branches or pull updates without
losing progress. You can later reapply those stashed changes.
Think of it like a clipboard or shelf: you put your work there, do
something else, and then bring it back when you’re ready.
Common Git Stash Commands
1. Save your changes
git stash
This saves staged and unstaged changes, then resets your working
directory to a clean state.
2. Save with a message
git stash save "WIP: login feature"
Adds a label so you know what you stashed.
3. View stash list
git stash list
Example output:
stash@{0}: WIP on feature/login
stash@{1}: WIP on bugfix/header
4. Apply the latest stash
git stash apply
This reapplies the most recent stash, but keeps it in the stash list.
5. Apply a specific stash
git stash apply stash@{1}
6. Pop a stash
git stash pop
Reapplies the most recent stash and removes it from the stash list.
7. Drop a stash
git stash drop stash@{0}
Deletes a specific stash entry.
8. Clear all stashes
git stash clear
Deletes all stashes at once.
Advanced Usage
-
Stash only staged changes:
git stash --keep-index
-
Stash including untracked files:
git stash -u
-
Stash including ignored files:
git stash -a
-
Apply stash to a new branch:
git stash branch new-feature
Creates a branch from the stash and switches to it.
When to Use Git Stash
✅ When you need to quickly switch branches without committing.
✅ When you want to pull new changes but your working directory is
dirty.
✅ When you are experimenting with changes but not ready to commit them.
❌ Avoid using git stash
as a replacement for commits — it’s only
temporary storage.
Summary
-
git stash
is like a temporary shelf for unfinished work. - Use
stash list
,stash apply
, andstash pop
to manage your
stashes. - Great for context switching without committing half-done work.