Master Git Stash Techniques: Save & Manage Changes
What is Git Stash?
Git stash is a powerful feature that allows you to temporarily save uncommitted changes without committing them to a branch. Think of it as a "shelf" where you can put your work aside, switch contexts, and come back to it later.
Simple Analogy: Imagine you're writing a document and need to work on something else urgently. Instead of saving a copy somewhere, you use a "stash drawer" to temporarily store your current work, clear your desk, and retrieve it exactly as you left it when ready to continue.
When to Use Git Stash
- Context Switching: Quickly switch branches with uncommitted work
- Emergency Fixes: Handle urgent bugs without losing current progress
- Testing Changes: Test different approaches without committing
- Clean Working Directory: Stash changes before pulling/merging
- Work Organization: Temporarily store incomplete features
Pro Tip: Stash is perfect for short-term storage. For longer-term storage, consider creating a feature branch instead.
Basic Stash Commands
Essential Commands
| Command | Description | Example |
|---|---|---|
git stash |
Save uncommitted changes (both staged and unstaged) | git stash |
git stash save "message" |
Save with a descriptive message | git stash save "login form WIP" |
git stash list |
Show all stashes | git stash list |
git stash pop |
Apply and remove latest stash | git stash pop |
git stash apply |
Apply latest stash without removing | git stash apply |
git stash drop |
Delete latest stash | git stash drop |
git stash clear |
Delete all stashes | git stash clear |
Practical Workflow Examples
git status
# Shows modified: file1.js, file2.js
# Urgent bug fix needed
git stash
# Working directory now clean
# Switch to main for hotfix
git checkout main
git checkout -b hotfix/login-bug
# ... make changes ...
git add .
git commit -m "Fix login validation bug"
# Return to original feature
git checkout feature-a
git stash pop
# Changes restored, stash removed
Advanced Stash Techniques
1. Stashing Specific Changes
You don't have to stash everything. Use -p (patch) to selectively stash:
# Git will ask for each change:
Stash this hunk [y,n,q,a,d,e,?]?
# Options:
# y - stash this hunk
# n - don't stash this hunk
# q - quit (stash selected)
# a - stash this and all remaining
# d - don't stash this and all remaining
2. Stashing Only Unstaged Changes
Sometimes you want to keep staged changes:
git stash --keep-index
# Or use more intuitive command
git stash push -m "message"
# Verify staged changes remain
git status
3. Stashing with Message
Always use descriptive messages for better organization:
git stash save "WIP: login form validation"
# Modern way (Git 2.13+)
git stash push -m "login form validation"
# View with messages
git stash list
# stash@{0}: On feature/login: login form validation
# stash@{1}: On main: experimental changes
4. Managing Multiple Stashes
Git maintains a stack of stashes. Learn to navigate them:
git stash list --date=relative
# Apply specific stash by index
git stash apply stash@{2}
# Show what's in a stash
git stash show stash@{1} -p
# Drop specific stash
git stash drop stash@{1}
# Create branch from stash
git stash branch new-branch stash@{0}
5. Stashing Untracked Files
By default, stash ignores new files. Include them with -u:
git stash -u
# Stash everything including ignored files
git stash -a
# Modern equivalent
git stash push -u -m "includes new files"
Caution: Using -a (all) includes ignored files, which might include build artifacts or sensitive data. Use with care.
Use Cases & Best Practices
Branch Switching
Quickly switch between branches without committing half-done work. Perfect for multitasking developers.
git checkout other-branch
# Do work, then return
git checkout original-branch
git stash pop
Emergency Bug Fixes
Handle production issues immediately without losing your current feature work.
git checkout -b hotfix
# Fix bug, test, deploy
git checkout feature/login
git stash pop
Clean Pull/Merge
Stash changes before pulling updates to avoid merge conflicts in your working directory.
git pull origin main
git stash pop
# Resolve any conflicts that arise
Stash vs. Other Git Features
| Feature | When to Use | Pros | Cons |
|---|---|---|---|
| Git Stash | Temporary storage (hours/days), quick context switches | Fast, no commits needed, preserves work state | Not versioned, can be lost, no remote backup |
| Temporary Commit | Need to share or backup work | Versioned, can be pushed, permanent record | Pollutes history, requires cleanup |
| New Branch | Long-term work, major feature development | Proper version control, easy to share | Overhead for small tasks |
| Git Worktree | Parallel development on same repo | No stash needed, separate directories | Disk space, Git 2.5+ required |
Stash Command Reference
Basic Operations
git stash push -m "Descriptive message"
# List all stashes
git stash list
# Apply latest stash and remove it
git stash pop
# Apply latest stash (keep in stack)
git stash apply
Advanced Operations
git stash show stash@{n} -p
# Create branch from stash
git stash branch branch-name stash@{n}
# Drop specific stash
git stash drop stash@{n}
# Clear all stashes
git stash clear
Partial Stashing
git stash -p
# Stash only staged changes
git stash --staged
# Stash with include untracked
git stash -u
# Stash specific files
git stash push file1.js file2.js
Common Pitfalls & Solutions
1. Stash Conflicts on Pop/Apply
When applying stash causes conflicts:
git status # See conflicted files
# Resolve conflicts manually
# Edit files, remove conflict markers
# After resolving:
git add resolved-file.js
# If using pop, stash is automatically dropped
# If using apply, manually drop stash:
git stash drop
2. Lost Stashes
Stashes can be recovered if you act quickly:
git fsck --unreachable | grep commit
# Find stash commits
git log --oneline --graph --all | grep -i stash
# Recover specific stash commit
git checkout abc1234 -- .
# Or use reflog
git reflog | grep stash
Important: Stashes are local only and can be garbage collected. Regularly convert important stashes to branches.
3. Stash Across Branches
Understanding how stashes work with branches:
git stash save "login validation"
# Can apply to any branch
git checkout main
git stash apply
# But be careful! The stash contains
# the exact files from original branch
# which may not make sense elsewhere
Frequently Asked Questions
Stashes are temporary and can be lost in several ways:
- Git garbage collection: Runs automatically to clean up unreachable objects
- Manual cleanup:
git stash clearremoves all stashes - Repository corruption: Rare but possible
- Expiration: Stashes don't expire automatically but can be GC'd
Best practices to prevent loss:
- Convert important stashes to branches:
git stash branch feature-name stash@{0} - Regularly review and clean up old stashes
- Use descriptive messages to remember what each stash contains
- Consider committing instead of stashing for long-term work
The key difference is whether the stash remains in the stack:
- git stash pop: Applies the stash AND removes it from the stack
- git stash apply: Applies the stash but leaves it in the stack
When to use each:
| Use Case | Command | Why |
|---|---|---|
| One-time context switch | git stash pop |
Clean up after yourself, no leftover stashes |
| Testing stash on different branches | git stash apply |
Keep stash to try elsewhere |
| Applying same stash multiple times | git stash apply |
Useful for testing or manual merging |
| Certain application | git stash pop |
Prevents accidental re-application |
Yes, Git provides several options for selective stashing:
git stash --keep-index
# Stash only staged changes (Git 2.35+)
git stash --staged
# Stash everything except specific files
git stash push -- ":(exclude)package.json"
# Interactive stashing (choose hunks)
git stash -p
# Stash specific files
git stash push file1.js file2.css
Real-world example: You've staged some changes for commit but want to test something. Stash the unstaged changes, test, then restore.
Stashes are local only, but you can share them through several methods:
- Convert to branch and push:
git stash branch shared-feature stash@{0}
git push -u origin shared-feature - Create patch file:
git stash show -p stash@{0} > my-stash.patch
# Send patch file to teammate
git apply my-stash.patch - Create temporary commit:
# Stash, create temp branch, commit
git stash
git checkout -b temp-share
git stash pop
git add .
git commit -m "WIP for sharing"
git push origin temp-share
Note: For ongoing collaboration, consider using feature branches instead of stashes.
Managing multiple stashes effectively:
- Use descriptive messages:
git stash push -m "login: form validation WIP" - Regular cleanup: Review stashes weekly with
git stash list - Convert to branches: For stashes older than a few days:
git stash branch stash-name stash@{n} - Tag important stashes: Create tags for reference:
git tag saved/login-validation stash@{0}^ - Use aliases for common operations:
# Add to .gitconfig
[alias]
sl = stash list
ss = stash save
sp = stash pop
sd = stash drop
sbr = stash branch
Golden rule: If a stash is important enough to keep for more than a week, it should probably be a branch.