💾 Save Work, Switch Branch, Come Back
Halfway through feature. Emergency bug fix needed. Don’t want to commit messy code. git stash saves work temporarily.
The Problem
# Working on feature branch git status # modified: feature.js # modified: styles.css # Boss: "Fix production bug NOW!" git checkout main # Error: Your local changes would be overwritten # Please commit or stash them # Options: # 1. Commit messy half-done work ❌ # 2. Create throwaway commit ❌ # 3. Copy files somewhere else ❌ # 4. Use git stash ✅
Basic Stash Usage
# Save current changes git stash # Saved working directory and index state # Working directory now clean git status # nothing to commit, working tree clean # Switch to other branch git checkout main # Fix bug, commit, push # Go back to feature branch git checkout feature-branch # Restore stashed changes git stash pop # Changes restored, stash removed # Continue working!
🎯 Essential Stash Commands
# Save with descriptive message
git stash save "WIP: user authentication form"
# List all stashes
git stash list
# stash@{0}: WIP: user authentication form
# stash@{1}: WIP: homepage redesign
# stash@{2}: On main: quick fix
# Apply stash without removing it
git stash apply stash@{0}
# Apply and remove (like pop)
git stash pop stash@{0}
# Show what's in a stash
git stash show stash@{0}
# Show full diff
git stash show -p stash@{0}
# Delete specific stash
git stash drop stash@{0}
# Delete all stashes
git stash clear
Advanced Stash Tricks
# Stash only staged changes
git stash --staged
# Stash including untracked files
git stash -u
# Stash everything (including ignored files)
git stash -a
# Stash specific files only
git stash push -m "message" path/to/file.js
# Create branch from stash
git stash branch new-feature-branch stash@{0}
# Creates branch, applies stash, drops stash
# Perfect when stash has conflicts with current branch
# Stash interactively (choose what to stash)
git stash -p
# Goes through each change, asks: stash this hunk?
🔄 Real Workflow
# Working on feature $ vim feature.js # Emergency: fix production bug $ git stash save "WIP: user login form validation" $ git checkout main $ git pull # Create hotfix branch $ git checkout -b hotfix-payment-bug $ vim payment.js $ git add payment.js $ git commit -m "Fix: payment timeout issue" $ git push origin hotfix-payment-bug # Back to feature $ git checkout feature-branch $ git stash pop # Continue where you left off!
Stash vs Commit
✅ When to Stash
- Temporary context switch
- Work in progress, not ready
- Experimenting, might discard
- Quick bug fix interruption
✅ When to Commit
- Logical unit of work done
- Want to keep in history
- Ready for code review
- Stable checkpoint
💡 Pro Tips
- Use descriptive messages: Future you will thank you
- Don’t hoard stashes: Clean up old ones regularly
- Stash before pull: Avoid merge conflicts with uncommitted work
- Create alias:
git config --global alias.save 'stash save'
⚠️ Common Mistakes
- Forgetting about stashes: Check
git stash listregularly - Stashing too much: Multiple stashes get confusing
- Not using messages:
stash@{3}tells you nothing - Conflicts on pop: Resolve like merge conflicts, then drop stash manually
“Before git stash, interruptions meant messy commits or lost work. Now I stash, fix bugs, come back instantly. Saved me dozens of times during on-call weeks.”
