Git: Stash Guide
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. This is useful when you need to quickly switch context and work on something else without committing half-done work.
What Gets Stashed?
By default, running git stash will stash:
Changes that have been added to your index (staged changes)
Changes made to files that are currently tracked by Git (unstaged changes)
But it will not stash:
New files in your working copy that have not yet been staged
Files that have been ignored
Stashing Changes
Basic Stash Commands
Save your current changes:
git stash
Save with a descriptive message:
git stash save "your descriptive message"
Stashing Untracked Files
To include untracked files in your stash, use the -u or --include-untracked option:
git stash --include-untracked
# or
git stash -u
Stashing All Files (Including Ignored)
To stash everything, including ignored files:
git stash --all
# or
git stash -a
Viewing Stashes
List All Stashes
View all stashed changes:
git stash list
Output example:
stash@{0}: WIP on master: 5002d47 our new homepage
stash@{1}: WIP on master: 5002d47 feat: add new feature
stash@{2}: WIP on master: 5002d47 fix: resolve bug
Show Latest Stash
View the latest stash summary:
git stash show
View the latest stash with full diff:
git stash show -p
View the latest stash including untracked files:
git stash show -u
# or
git stash show --include-untracked
View full diff with untracked files:
git stash show -p -u
View only untracked files in latest stash:
git stash show -p --only-untracked
Show Specific Stash
Replace stash@{1} with your desired stash reference:
View specific stash summary:
git stash show stash@{1}
View specific stash with full diff:
git stash show stash@{1} -p
View specific stash including untracked files:
git stash show stash@{1} -u
# or
git stash show stash@{1} --include-untracked
View only untracked files in specific stash:
git stash show stash@{1} --only-untracked
Re-applying Stashed Changes
Git Stash Pop
Reapply the most recent stash and remove it from the stash list:
git stash pop
Important
git stash pop removes the changes from your stash and reapplies them to your working copy.
Pop a specific stash:
git stash pop stash@{1}
Git Stash Apply
Reapply stashed changes but keep them in the stash list:
git stash apply
This is useful when you want to apply the same stashed changes to multiple branches.
Apply a specific stash:
git stash apply stash@{1}
Apply stash and restore staged state:
git stash apply --index
Deleting Stashes
Drop a Single Stash
Remove a specific stash from the stash list:
git stash drop stash@{2}
Drop the latest stash:
git stash drop
Clear All Stashes
Remove all stashed entries:
git stash clear
Warning
This action cannot be undone. All stashed changes will be permanently deleted.
Creating a Branch from Stash
When you want to create a new branch and apply your stashed changes to it:
Create a branch from the latest stash:
git stash branch <branch_name>
Create a branch from a specific stash:
git stash branch <branch_name> stash@{1}
This is particularly useful when:
You stashed work on the wrong branch
You need to create a feature branch from stashed changes
You want to isolate stashed work into its own branch
Advanced Usage
Stash with Patch Mode
Interactively select which changes to stash:
git stash push -p
# or
git stash save -p
Stash Specific Files
Stash only specific files:
git stash push -m "message" path/to/file1 path/to/file2
Stash with Keep Index
Stash changes but keep staged files in the index:
git stash --keep-index
View Stash as Diff
View the difference between current state and a stash:
git diff stash@{0}
Common Workflows
Scenario 1: Quick Context Switch:
# You're working on a feature but need to fix a bug urgently
git stash save "WIP: feature in progress"
git checkout -b hotfix/urgent-bug
# Fix the bug and commit
git checkout feature-branch
git stash pop
Scenario 2: Testing Changes on Different Branches:
# You have changes you want to test on multiple branches
git stash save "experimental changes"
git checkout branch1
git stash apply
# Test changes
git checkout branch2
git stash apply
# Test again
# When done, clean up
git stash drop
Scenario 3: Cleaning Working Directory:
# Save all changes including untracked files
git stash -u
# Now you have a clean working directory
# Later restore everything
git stash pop
Quick Reference Cheat Sheet
Stashing Commands
Command |
Description |
|---|---|
|
Stash tracked changes |
|
Stash with descriptive message |
|
Stash including untracked files |
|
Stash including ignored files |
|
Interactively stash changes |
Viewing Commands
Command |
Description |
|---|---|
|
List all stashes |
|
Show latest stash summary |
|
Show latest stash with full diff |
|
Show specific stash |
|
Show with untracked files |
Applying Commands
Command |
Description |
|---|---|
|
Apply latest stash and remove it |
|
Apply latest stash and keep it |
|
Apply specific stash |
|
Apply and restore staged state |
Deleting Commands
Command |
Description |
|---|---|
|
Delete latest stash |
|
Delete specific stash |
|
Delete all stashes |
Branch Commands
Command |
Description |
|---|---|
|
Create branch from latest stash |
|
Create branch from specific stash |
Best Practices
Use Descriptive Messages: Always use
git stash save "descriptive message"to make it easier to identify stashes later.Don’t Accumulate Stashes: Clean up old stashes regularly with
git stash droporgit stash clear.Be Careful with Pop: Use
git stash applyif you’re not sure you want to remove the stash yet.Stash Before Pulling: If you have uncommitted changes and need to pull, stash first to avoid conflicts.
Review Before Applying: Always use
git stash show -pto review changes before applying them.Use Branches for Long-term Storage: Stashes are meant for temporary storage. For longer-term work, create a branch instead.
Troubleshooting
Stash Pop Conflicts
If git stash pop results in conflicts:
Resolve the conflicts manually
Stage the resolved files:
git add <file>The stash will be automatically dropped after resolution
If you want to keep the stash:
git stash apply
# Resolve conflicts
git add <resolved-files>
# Stash is still available
Lost Stash Recovery
If you accidentally dropped a stash, you might be able to recover it using:
git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
Then inspect the commits to find your lost stash and apply it:
git stash apply <commit-hash>