Git Stashing
Stashing saves your uncommitted changes to a stack, letting you switch contexts quickly without committing work-in-progress.
Why Stash?
You're working on a feature when something urgent comes up:
bash1$ git status2Changes not staged for commit:3 modified: feature.js45$ git checkout main6error: Your local changes to the following files would be overwritten by checkout:7 feature.js8Please commit your changes or stash them before you switch branches.
You're not ready to commit. Stashing saves the day.
Basic Stashing
Stash Current Work
bash1$ git stash2Saved working directory and index state WIP on feature/login: abc1234 Add login form34$ git status5On branch feature/login6nothing to commit, working tree clean
Your changes are safely stored. Now you can switch branches.
Stash with Message
bash1$ git stash push -m "WIP: login validation logic"2Saved working directory and index state On feature/login: WIP: login validation logic
Messages help identify stashes later.
Viewing Stashes
List All Stashes
bash1$ git stash list2stash@{0}: On feature/login: WIP: login validation logic3stash@{1}: WIP on main: def5678 Update deps4stash@{2}: On feature/api: WIP: API refactor
Stashes are numbered like a stack. stash@{0} is the most recent.
Show Stash Contents
bash1# Most recent stash2$ git stash show3 feature.js | 15 +++++++++------4 1 file changed, 9 insertions(+), 6 deletions(-)56# Show diff7$ git stash show -p89# Specific stash10$ git stash show stash@{2}
Restoring Stashed Work
Apply (Keep Stash)
bash1$ git stash apply2On branch feature/login3Changes not staged for commit:4 modified: feature.js
The changes are restored, but the stash remains in the list.
Pop (Apply and Remove)
bash1$ git stash pop2On branch feature/login3Changes not staged for commit:4 modified: feature.js56Dropped refs/stash@{0} (abc123...)
The stash is applied and removed from the list.
Apply Specific Stash
bash1$ git stash apply stash@{2}
Selective Stashing
Stash Specific Files
bash1$ git stash push -m "just feature.js" feature.js
Interactive Stashing
Choose which hunks to stash:
bash1$ git stash push -p
Similar to git add -p, you'll be prompted for each change.
Include Untracked Files
By default, stash ignores untracked files:
bash1$ git stash push -u -m "include new files"2# or3$ git stash push --include-untracked
Include Ignored Files Too
bash1$ git stash push -a -m "everything including ignored"2# or3$ git stash push --all
Managing Stashes
Delete a Stash
bash1$ git stash drop stash@{1}2Dropped stash@{1} (abc123...)
Clear All Stashes
bash1$ git stash clear
Warning: This is permanent! All stashes are deleted.
Create Branch from Stash
If a stash causes conflicts when applying, create a branch:
bash1$ git stash branch new-feature-branch stash@{0}2Switched to a new branch 'new-feature-branch'3On branch new-feature-branch4Changes not staged for commit:5 modified: feature.js67Dropped refs/stash@{0} (abc123...)
This creates a new branch at the commit where you stashed, applies the stash, and drops it.
Common Workflows
Quick Context Switch
bash1# Save current work2$ git stash push -m "WIP: feature X"34# Handle urgent task5$ git checkout main6$ git checkout -b hotfix/urgent7# ... fix bug ...8$ git commit -am "fix: urgent bug"9$ git push1011# Return to feature12$ git checkout feature/x13$ git stash pop
Review Changes Before Stashing
bash1# Check what you're about to stash2$ git diff34# Stash it5$ git stash push -m "checked and stashed"
Stash While Rebasing
If a rebase conflicts with uncommitted changes:
bash1$ git stash2$ git rebase main3$ git stash pop
Stash Pitfalls
Stash Conflicts
If your codebase changed significantly, applying a stash may conflict:
bash1$ git stash pop2Auto-merging feature.js3CONFLICT (content): Merge conflict in feature.js
Resolve the conflict, then the stash won't auto-drop. Clean up manually:
bash1$ git stash drop
Lost Stashes
Stashes can be lost if you:
- Run
git stash clear - Drop them accidentally
- Never apply before they expire (rare)
Tip: For important work, commit to a branch instead of stashing long-term.
Commands Summary
| Command | Purpose |
|---|---|
git stash | Stash changes (auto message) |
git stash push -m "msg" | Stash with message |
git stash push -u | Include untracked files |
git stash list | List all stashes |
git stash show | Show stash summary |
git stash show -p | Show stash diff |
git stash apply | Apply, keep stash |
git stash pop | Apply and remove stash |
git stash drop | Delete a stash |
git stash clear | Delete all stashes |
git stash branch <name> | Create branch from stash |
Key Takeaway
Stashing is for quick context switches—minutes, not days. For longer parallel work, use worktrees or proper branches. Keep stash messages descriptive, and don't let stashes pile up.