Git Stash
Master Git stash to temporarily save work, switch contexts quickly, and manage multiple tasks efficiently
Git Stash: Your Development Safety Net
Git stash is your secret weapon for handling interruptions and context switches during development. Whether you need to quickly fix a bug, switch branches, or pull updates, stash lets you temporarily save uncommitted work without creating messy commits.
Understanding Git Stash
What is Git Stash?
Git stash temporarily saves your uncommitted changes and reverts your working directory to the last commit state. Think of it as a "clipboard" for your work-in-progress.
When to Use Stash
- Context switching: Need to quickly work on different branch
- Pulling updates: Working directory must be clean for
git pull
- Emergency fixes: Urgent bug requires immediate attention
- Experimentation: Try different approaches without losing current work
- Code reviews: Temporarily save changes while reviewing others' code
What Gets Stashed
By default, git stash
saves:
- ✅ Modified tracked files
- ✅ Staged changes
- ❌ Untracked files (new files)
- ❌ Ignored files
Basic Stashing Operations
Creating Stashes
# Basic stash (saves staged and unstaged changes)
git stash
# Stash with descriptive message (recommended)
git stash push -m "Work in progress: user authentication feature"
# Modern syntax (Git 2.13+)
git stash push -m "Feature: add login validation"
# Legacy syntax (still works)
git stash save "Feature: add login validation"
Listing Stashes
# View all stashes
git stash list
# Output example:
stash@{0}: On feature-login: Work in progress: user authentication
stash@{1}: WIP on main: 2f8a9b1 Fix navigation bug
stash@{2}: On hotfix: Emergency database connection fix
Applying Stashes
# Apply most recent stash (keeps stash in list)
git stash apply
# Apply specific stash by index
git stash apply stash@{1}
# Apply and remove stash (most common)
git stash pop
# Pop specific stash
git stash pop stash@{0}
Advanced Stashing Techniques
Stashing Specific Files
# Stash only specific files
git stash push -m "Style updates" src/styles.css src/layout.css
# Stash everything except specific files
git stash push -m "All except config" -- . ':!config/settings.js'
# Interactive stashing (choose hunks)
git stash push -p -m "Partial feature implementation"
Stashing Untracked Files
# Include untracked files in stash
git stash push -u -m "Include new files"
# or
git stash push --include-untracked -m "With new files"
# Include everything (tracked, untracked, and ignored)
git stash push -a -m "Complete workspace snapshot"
# or
git stash push --all -m "Everything included"
Stashing Staged Changes Only
# Stash only staged changes, keep unstaged changes
git stash push --staged -m "Only staged changes"
# Keep staged changes, stash unstaged changes
git stash push --keep-index -m "Keep staged, stash unstaged"
Working with Multiple Stashes
Stash Management
# Create multiple named stashes
git stash push -m "Feature A: halfway done"
git stash push -m "Feature B: needs testing"
git stash push -m "Bugfix: temporary solution"
# View stash contents without applying
git stash show stash@{0}
# View detailed diff of stash
git stash show -p stash@{1}
# View stash summary
git stash show --stat stash@{0}
Stash Operations
# Drop (delete) specific stash
git stash drop stash@{1}
# Clear all stashes (use with caution!)
git stash clear
# Create branch from stash
git stash branch new-feature-branch stash@{0}
# Apply stash to different branch
git checkout other-branch
git stash apply stash@{0}
Real-World Workflows
Emergency Bug Fix Workflow
# Scenario: Working on feature when urgent bug reported
# 1. Stash current work
git stash push -m "WIP: user profile feature - 80% complete"
# 2. Switch to main branch
git checkout main
# 3. Create hotfix branch
git checkout -b hotfix/login-error
# 4. Fix the bug and commit
# ... make fixes ...
git add .
git commit -m "fix: resolve login timeout issue"
# 5. Push hotfix
git push -u origin hotfix/login-error
# 6. Return to feature work
git checkout feature/user-profile
git stash pop
Context Switching Workflow
# Scenario: Need to review PR while working on feature
# 1. Stash incomplete work
git stash push -m "Feature: payment integration - needs validation"
# 2. Fetch and checkout PR branch for review
git fetch origin pull/123/head:pr-123
git checkout pr-123
# 3. Review, test, and provide feedback
# ... review code ...
# 4. Return to your work
git checkout feature/payment-integration
git stash pop
Multiple Task Management
# Managing multiple features simultaneously
# Work on Feature A
git checkout -b feature/user-dashboard
# ... make changes ...
git stash push -m "Dashboard: basic layout complete"
# Switch to Feature B
git checkout -b feature/email-notifications
# ... make changes ...
git stash push -m "Notifications: email templates done"
# Quick bug fix
git checkout main
# ... fix bug ...
git add . && git commit -m "fix: header alignment"
# Back to Feature A
git checkout feature/user-dashboard
git stash pop # Resumes dashboard work
Stash Conflicts and Resolution
Handling Stash Conflicts
# If applying stash creates conflicts
git stash pop
# Auto-merging src/app.js
# CONFLICT (content): Merge conflict in src/app.js
# 1. Resolve conflicts manually
vim src/app.js # Edit file to resolve conflicts
# 2. Stage resolved files
git add src/app.js
# 3. Note: Stash is automatically dropped after successful pop
# If pop failed, the stash remains in the list
git stash list # Check if stash still exists
# If you used 'apply' and want to remove the stash after resolving:
git stash drop stash@{0}
Preventing Conflicts
# Before applying stash, check for potential conflicts
git stash show -p | git apply --check
# If conflicts expected, create new branch for stash
git stash branch resolve-conflicts stash@{0}
Advanced Stash Scenarios
Selective Stash Application
# Apply only specific files from stash
git checkout stash@{0} -- src/utils.js src/helpers.js
# Cherry-pick changes from stash
git stash show -p stash@{0} | git apply --index
# Apply stash without staging changes
git stash show -p | git apply
Stash Inspection
# View what's in a stash without applying
git stash show stash@{0} # Summary
git stash show -p stash@{0} # Full diff
git stash show --stat stash@{0} # File statistics
# View stash as commit
git show stash@{0}
# View only specific file from stash
git show stash@{0}:path/to/file.js
Stash Backup and Transfer
# Create branch from stash for backup
git stash branch backup-feature-work stash@{0}
# Transfer stash to different repository
git stash show -p stash@{0} > /tmp/mystash.patch
# In other repo:
git apply /tmp/mystash.patch
# Export all stashes
for stash in $(git stash list | cut -d: -f1); do
git stash show -p $stash > "$stash.patch"
done
Stash Best Practices
Do's ✅
- Use descriptive messages: Always include context about what's stashed
- Stash frequently: Better to have too many stashes than lose work
- Clean up regularly: Remove stashes you no longer need
- Test after applying: Always verify stashed changes work correctly
- Use branches for long-term storage: Stashes are temporary solutions
Don'ts ❌
- Don't rely on stash for version control: Use commits for permanent storage
- Don't stash sensitive data: Use proper secrets management
- Don't forget about stashes: Regular cleanup prevents confusion
- Don't stash everything: Be selective about what needs stashing
- Don't use stash as a commit alternative: Stash is for temporary storage
Naming Conventions
# Good stash messages
git stash push -m "WIP: user authentication - 70% complete"
git stash push -m "Feature: shopping cart - needs testing"
git stash push -m "Bugfix: memory leak investigation"
git stash push -m "Experiment: new UI layout approach"
# Poor stash messages
git stash push -m "stuff"
git stash push -m "wip"
git stash push -m "changes"
Troubleshooting Common Issues
"No local changes to save"
# Check what Git sees as changes
git status
git diff
git diff --staged
# If you want to stash untracked files:
git stash push -u -m "Include untracked files"
"Cannot apply stash"
# Check current branch state
git status
# Ensure working directory is clean
git add . && git commit -m "Commit current changes first"
# Then apply stash
git stash pop
# Or apply to different branch
git stash branch temp-branch stash@{0}
"Stash disappeared"
# Check reflog for lost stashes
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --merges --no-walk --grep=WIP
# Or check reflog directly
git log --graph --oneline --all $(git reflog | cut -c1-7)
Stash vs. Alternatives
Stash vs. Commit
Stash | Commit |
---|---|
Temporary storage | Permanent history |
Easy to lose | Tracked in history |
Quick and simple | Requires commit message |
Not shared | Can be pushed/shared |
Stash vs. Branch
Stash | Branch |
---|---|
Single workspace | Multiple workspaces |
Temporary | Permanent until deleted |
Stack-like (LIFO) | Named references |
Local only | Can be shared |
When to Use Each
# Use STASH for:
# - Quick context switches (< 1 hour)
# - Temporary experiments
# - Pulling updates with dirty working directory
# - Emergency interruptions
# Use COMMIT for:
# - Completed features/fixes
# - Checkpoint saves
# - Sharing work with others
# - Long-term storage
# Use BRANCH for:
# - Parallel feature development
# - Long-running experiments
# - Collaborative work
# - Different approaches to same problem
Quick Reference
Essential Commands
# Daily stash operations
git stash push -m "description" # Save with message
git stash list # View all stashes
git stash pop # Apply and remove latest
git stash apply stash@{n} # Apply specific stash
git stash drop stash@{n} # Delete specific stash
git stash clear # Delete all stashes
# Advanced operations
git stash push -u -m "msg" # Include untracked files
git stash push -p -m "msg" # Interactive stashing
git stash show -p stash@{0} # View stash contents
git stash branch new-branch # Create branch from stash
Workflow Patterns
Scenario | Commands |
---|---|
Quick switch | git stash → switch → work → return → git stash pop |
Emergency fix | git stash → git checkout main → fix → git checkout - → git stash pop |
Experiment | git stash push -p → experiment → git stash pop or discard |
Pull updates | git stash → git pull → git stash pop |
Next Steps
🎯 Congratulations! You now understand Git stash thoroughly.
Continue learning:
- Master Git Reset - Learn to undo changes safely
- Understand Git Revert - Safely undo committed changes
- Explore Git Rebase - Clean up commit history
Practice Exercises
- Create a stash with untracked files
- Practice applying stashes to different branches
- Resolve conflicts when applying stashes
- Use interactive stashing to save partial changes
- Create branches from stashes
- Set up a workflow for managing multiple tasks