Stashing Changes
Git stash temporarily shelves your changes so you can work on something else, then come back and reapply them later.
What is Stashing?
Stashing is like putting your work-in-progress in a drawer:
Working Directory Stash
┌────────────────┐ ┌────────────────┐
│ Modified │ │ │
│ files │ ──git stash──→│ Saved for │
│ (WIP) │ │ later │
└────────────────┘ └────────────────┘
↓ ↓
(clean state) (retrieve when ready)
When to Use Stash
| Scenario | Why Stash? |
|---|---|
| Switch branches mid-work | Can't switch with uncommitted conflicts |
| Quick hotfix needed | Save WIP, fix bug, return |
| Pull with local changes | Stash, pull, pop |
| Test without your changes | Temporarily remove WIP |
| Experiment | Save current state before trying something |
Basic Stashing
Stash Changes
# Stash tracked, modified files
git stash
# Stash with a message
git stash save "WIP: user authentication"
# or
git stash push -m "WIP: user authentication"
What Gets Stashed
By default, stash saves:
- Modified tracked files
- Staged changes
NOT stashed by default:
- Untracked files
- Ignored files
Include Untracked Files
# Include untracked files
git stash -u
git stash --include-untracked
# Include untracked AND ignored files
git stash -a
git stash --all
Viewing Stashes
# List all stashes
git stash list
Output:
stash@{0}: WIP on main: abc1234 Latest commit
stash@{1}: On feature: def5678 Working on login
stash@{2}: WIP on main: 789abcd Fix bug
The most recent stash is stash@{0}.
View Stash Contents
# Show summary of latest stash
git stash show
# Show full diff
git stash show -p
# Show specific stash
git stash show stash@{2}
git stash show -p stash@{2}
Retrieving Stashed Changes
Pop (Apply and Remove)
# Apply latest stash and remove it from stash list
git stash pop
# Pop specific stash
git stash pop stash@{2}
Apply (Keep in Stash)
# Apply latest stash but keep it in stash list
git stash apply
# Apply specific stash
git stash apply stash@{1}
Difference Between Pop and Apply
| Command | Action |
|---|---|
pop | Apply + delete stash |
apply | Apply, keep stash |
Use apply when you might need the stash again.
Stash Operations
Create Branch from Stash
# Create new branch and apply stash
git stash branch my-feature stash@{0}
This:
- Creates branch from commit where you stashed
- Applies the stash
- Drops the stash if successful
Drop a Stash
# Delete specific stash
git stash drop stash@{0}
# Delete all stashes
git stash clear
Partial Stashing
Stash Only Specific Files
# Stash only specific files
git stash push -m "stash readme" README.md
# Stash multiple files
git stash push -m "stash configs" config.js settings.json
Interactive Stashing
git stash -p
# or
git stash push -p
Git asks about each change:
Stash this hunk [y,n,q,a,d,s,e,?]?
y: Stash this hunkn: Don't stash this hunks: Split into smaller hunksq: Quit (stash selected hunks)
Stash and Stage
Keep Staged Changes
# Stash only unstaged changes (keep staged)
git stash --keep-index
Recreate Staging
# Apply stash and recreate staged/unstaged distinction
git stash apply --index
Common Workflows
Quick Context Switch
# Working on feature...
# Boss: "Fix production bug NOW!"
git stash push -m "WIP: feature work"
git checkout main
git pull
git checkout -b hotfix
# ... fix bug ...
git checkout feature-branch
git stash pop
# Continue feature work
Pull with Local Changes
# You have local changes, need to pull
git stash
git pull
git stash pop
# Resolve conflicts if any
Test Without Changes
# Temporarily remove your changes
git stash
# Run tests with clean code
npm test
# Restore your changes
git stash pop
Exercise: Stash Practice
Practice stashing and retrieving changes:
Stash Conflicts
If applying a stash causes conflicts:
git stash pop
# CONFLICT!
# Resolve conflicts
# Edit conflicted files
git add resolved-file.js
# The stash is NOT automatically dropped
# Drop it manually if resolved
git stash drop
Best Practices
Use Descriptive Messages
# Bad
git stash
# Good
git stash push -m "WIP: user form validation, needs testing"
Don't Let Stashes Pile Up
Stashes are temporary. Use them, don't hoard them:
# Check your stash list regularly
git stash list
# Clean old stashes
git stash drop stash@{5}
Consider Committing Instead
For longer interruptions, commit your WIP:
# Create WIP commit
git commit -am "WIP: work in progress"
# Later, undo the commit but keep changes
git reset --soft HEAD~1
Summary
- Stash temporarily saves uncommitted changes
- Use
git stashto stash,git stash popto retrieve - Include untracked files with
-u - Use messages with
-m "description" - View stashes with
git stash list - Use
applyto keep stash after applying - Use
stash -pfor interactive, partial stashing - Don't let stashes accumulate—use them or commit
In the next module, we'll explore GitHub-specific features.

