Git Stashing - Save Work in Progress
What is Git Stashing?
Git stashing allows you to temporarily save your uncommitted changes (both staged and unstaged) without creating a commit. This is useful when you need to switch branches or pull changes from remote without losing your work in progress.
The stash is a stack of saved changes that you can apply later.
When to Use Stashing
- You're working on a feature but need to urgently switch to another branch
- You need to pull remote changes that conflict with your work
- You want to clean your working directory temporarily
- You're experimenting and want to save the experiment without committing
Basic Stashing
Save Your Work
git stash
This command:
- Saves all staged and unstaged changes
- Reverts your working directory to the last commit
- Your changes are safely stored in the stash
git stash save "WIP: implementing user authentication"
# or the newer syntax
git stash push -m "WIP: implementing user authentication"
Descriptive messages make it easier to find the right stash later.
View All Stashes
git stash list
Output example:
stash@{0}: WIP: implementing user authentication
stash@{1}: WIP: fixing login bug
stash@{2}: WIP: updating database schema
Each stash has an identifier like stash@{0}. The most recent stash is stash@{0}.
See Stash Details
git stash show stash@{0}
This shows a summary of changed files.
git stash show -p stash@{0}
The -p flag shows the diff of all changes in the stash.
Applying Stashed Changes
Apply and Keep Stash
git stash apply
# or apply a specific stash
git stash apply stash@{0}
This reapplies the changes but leaves the stash in the stash list.
If applying a stash causes conflicts, Git will mark conflicted files. You need to resolve conflicts manually before continuing.
Apply and Remove Stash
git stash pop
# or
git stash pop stash@{1}
This applies the stash and removes it from the list. This is the most common workflow.
Examples
Scenario 1: Quick branch switch
# You're on feature-branch with uncommitted changes
git stash
git checkout main
git checkout feature-branch
git stash pop
Scenario 2: Pull changes safely
# Your changes conflict with remote
git stash
git pull origin main
git stash pop
# Resolve any conflicts that arise
Stash Management
Delete Stashes
git stash drop stash@{0}
git stash clear
Dropped stashes cannot be recovered. Use with caution!
Create Branch from Stash
Convert a stash into a new branch:
git stash branch new-feature
This creates a new branch and applies the stash to it. Useful if you realize your stashed work deserves its own branch.
Advanced Stashing
Stash Only Staged Changes
git stash --keep-index
# or the newer syntax
git stash --staged
This stashes only changes you've staged with git add, leaving unstaged changes in your working directory.
# You have both staged and unstaged changes
git stash push -u -m "WIP: unstaged changes"
# This stashes both untracked and unstaged files
Stash Untracked Files
git stash -u
# or
git stash push -u
By default, git stash ignores untracked files. Use -u to include them.
git stash -a
The -a flag includes even ignored files.
Stash Specific Files
git stash push path/to/file1 path/to/file2 -m "WIP: specific files"
Practical Workflows
Workflow 1: Interrupting Work for Urgent Task
# You're implementing a new feature
vim feature.js
git status
# On branch feature-development
# Changes not staged for commit:
# modified: feature.js
# Critical bug reported - need to switch to main
git stash save "WIP: implementing new feature"
# Now working on bug fix
git checkout main
git checkout -b hotfix/critical-bug
vim bug.js
git add bug.js
git commit -m "Fix: critical production bug"
git checkout main
git merge hotfix/critical-bug
# Back to your feature work
git checkout feature-development
git stash pop
Workflow 2: Clean Workspace for Testing
# You have experimental changes
git status
# Save and clean up
git stash save "WIP: experimental changes"
# Test with clean state
npm test
# All tests pass, restore your work
git stash pop
Workflow 3: Handling Merge Conflicts with Stash
# Your branch with uncommitted work
git stash save "WIP: current work"
# Pull latest from main
git fetch origin
git rebase origin/main
# (might have conflicts)
# Rebase complete, restore your work
git stash pop
# (might have conflicts)
# Resolve any conflicts
git status
# Fix conflicts in files
git add resolved-files
git commit -m "Resolve merge conflicts"
Common Issues
Issue: Stash Pop with Conflicts
If git stash pop causes conflicts:
# Stash pop created conflicts
# 1. Fix the conflicted files manually
vim conflicted-file.js
# 2. Stage the resolved files
git add conflicted-file.js
# 3. You still need to drop the stash manually
git stash drop stash@{0}
# Alternative: Use apply then drop
git stash apply stash@{0}
# (resolve conflicts)
git add .
git stash drop stash@{0}
Issue: Finding an Old Stash
# Search stash messages
git stash list | grep "search term"
# See changes in all stashes
git stash show -p | grep "specific code"
Issue: Accidentally Deleted Stash
# See recent deletions
git reflog
# Look for 'stash@{..}' entries with 'drop'
# Recover the stash
git stash apply <hash-from-reflog>
Best Practices
- Use descriptive messages:
git stash save "WIP: message"instead of unnamed stashes - Don't stash long-term: Stash is temporary, commit or clean up stashes regularly
- Review stash before applying: Use
git stash show -pbeforegit stash pop - Clear old stashes: Periodically run
git stash listand clean up old stashes - Create branches for important work: If you stash something multiple times, consider creating a branch instead
- Prefer switching branches early: Stash is great for quick switches, but plan your branches ahead
Stashing vs Committing
| Scenario | Stash | Commit |
|---|---|---|
| Temporary WIP changes | ✅ | ❌ |
| Need to switch branches | ✅ | ✅ |
| Want to save for others to see | ❌ | ✅ |
| Don't have a final solution yet | ✅ | ❌ |
| Changes are tested and ready | ❌ | ✅ |
Key Takeaways
git stashtemporarily saves uncommitted changesgit stash listshows all saved stashesgit stash popapplies and removes a stashgit stash applyapplies a stash without removing it- Use descriptive messages to track what each stash contains
- Stash is temporary—commit completed work to the repository
- Can create branches from stashes for longer-term work