Git Stash Cheatsheet — save / pop / apply / drop / list / show / branch
Git stash is your 'save game' for uncommitted work. It temporarily shelves changes so you can switch branches, pull upstream changes, or start fresh — then restore your work later. This guide covers everything from basic stash-and-pop to advanced operations like stashing untracked files, partial stashes, and creating branches from stashed work.
Quick Reference Table
| Operation | Command | Use Case |
| Stash all tracked changes | git stash | Quick save, switch branches |
| Stash with message | git stash push -m "msg" | Document what you saved |
| Stash untracked files too | git stash push -u | Include new files |
| Stash everything (including ignored) | git stash push -a | Full workspace clean |
| Stash specific files | git stash push -m "msg" | Selective save |
| Stash interactively (hunk-by-hunk) | git stash push -p | Partial stash |
| List all stashes | git stash list | See what's saved |
| Show a stash's diff | git stash show -p [stash@{n}] | Inspect stashed changes |
| Apply latest stash & keep it | git stash apply | Restore without dropping |
| Apply and drop (one shot) | git stash pop | Default restore flow |
| Apply a specific stash | git stash apply stash@{2} | Pick a particular save |
| Drop a specific stash | git stash drop stash@{2} | Clean up old stashes |
| Drop all stashes | git stash clear | ⚠️ Irreversible cleanup |
| Create a branch from a stash | git stash branch [stash@{n}] | Recover onto a new branch |
git stash push — Save Your Work
The git stash push command (or the shorthand git stash) saves modified tracked files to a new stash entry and reverts your working directory to the last commit's state.
``bash
# Basic save — just tracked files
git stash
# Save with a descriptive message git stash push -m "WIP: refactor auth middleware"
# Stash untracked files as well git stash push -u
# Stash everything, including gitignored files git stash push -a
# Stash only specific files git stash push -m "config tweaks" src/config.ts
# Stash interactively — choose hunks one by one git stash push -p
# Keep staged changes, stash only unstaged modifications
git stash push --keep-index
`
> Pro tip: git stash push is the modern, feature-rich form. The old git stash save command is deprecated.
### Options Breakdown
| Flag | Effect |
| (none) | Stash tracked files only |
-u / --include-untracked | Include untracked files |
-a / --all | Include ignored files too |
-m / --message | Attach a label for identification |
-p / --patch | Interactive hunk selection |
--keep-index | Leave staged changes alone |
git stash list / show — Inspect Stashes
`bash
# List all stash entries
git stash list
# Show summary with details git stash list --format="%gd: %gs (%cr)"
# Show what a stash changes (diffstat) git stash show stash@{0}
# Show full diff of a stash git stash show -p stash@{2}
# Show the latest stash's diff
git stash show -p
`
The git stash list output uses stash@{n} notation:
`
stash@{0}: On main: WIP: refactor auth middleware
stash@{1}: On feature/login: fix login form validation
stash@{2}: On develop: debug console output cleanup
`
The stash stack is LIFO (Last In, First Out). stash@{0} is always the most recent.
git stash pop / apply — Restore Changes
### git stash pop — Apply & Remove
Applies the stash and drops it from the stash list. This is the standard one-shot workflow.
`bash
# Apply latest stash and remove it
git stash pop
# Apply a specific stash and remove it
git stash pop stash@{1}
`
If conflicts arise during pop, the stash entry is not dropped — it's preserved so you can fix conflicts and try again.
### git stash apply — Apply & Keep
Applies the stash but keeps it in the stash list. Use this when you need to restore the same changes multiple times (e.g., cherry-pick a fix to several branches).
`bash
# Apply latest stash, keep it
git stash apply
# Apply a specific stash, keep it
git stash apply stash@{3}
`
| Feature | git stash pop | git stash apply |
| Restores changes | ✅ | ✅ |
| Drops from list | ✅ | ❌ |
| Use for one-time restore | ⭐⭐⭐⭐⭐ | ❌ |
| Use for multi-branch restore | ❌ | ⭐⭐⭐⭐⭐ |
| Behavior on conflict | Stash preserved | Stash preserved |
git stash drop / clear — Clean Up
`bash
# Drop a specific stash
git stash drop stash@{1}
# Drop the latest stash git stash drop
# Clear all stashes (⚠️ irreversible)
git stash clear
`
> ⚠️ Warning: git stash clear deletes every stash entry permanently. There is no git stash undo. If you're unsure, drop individual stashes instead.
git stash branch — Recover onto a New Branch
If you stash changes on one branch and the working tree has drifted significantly, applying the stash can cause painful conflicts. git stash branch creates a new branch starting at the commit where the stash was created.
`bash
# Create a branch from the latest stash
git stash branch fix/login-issue
# Create a branch from a specific stash
git stash branch fix/login-issue stash@{1}
`
This command:
1. Checks out the commit the stash was based on
2. Creates a new branch at that commit
3. Applies the stash onto that branch
4. Drops the stash entry (like pop)
This is the safest way to recover work from an old stash that no longer cleanly applies to current main.
Practical Workflow Examples
### Example 1: Interrupt — switch to another branch urgently
`bash
# You're working on feature-x, but a hotfix on main needs attention
git stash push -m "WIP: feature-x progress"
git checkout main
# ... fix the bug, commit, push ...
git checkout feature-x
git stash pop
`
### Example 2: Pull without committing
`bash
# Need to git pull but have dirty working tree
git stash push -m "before pull"
git pull --rebase
git stash pop
`
### Example 3: Clean workspace for a clean build
`bash
git stash push -u -m "clean build checkpoint"
./build.sh
git stash pop
`
### Example 4: Recover old stash that won't apply cleanly
`bash
git stash list
# stash@{4}: On adjust-pricing: price calculation rework
git stash branch rework-pricing stash@{4}
# Now working in a fresh branch at the original commit
`
### Example 5: Test with and without your changes
`bash
# Save changes
git stash push -m "my feature changes"
npm test # run tests without your changes
git stash pop
npm test # run tests with your changes
`
### Example 6: Stash only staged changes for a partial commit
`bash
git add src/app.ts # stage the 'safe' changes
git stash push --keep-index # stash the rest of the messy work
git commit -m "safe commit"
git stash pop # bring back the messy work
`
Common Troubleshooting
| Symptom | Cause | Fix |
git stash apply fails with conflicts | Working tree has diverged | Use git stash branch instead |
git stash didn't save my new files | Untracked files excluded | Use git stash push -u |
| Accidentally cleared my stash | Stash list is now empty | Check with git stash list; if truly empty, it's gone — stash in regular intervals as a habit |
| Want to see what's in a stash without applying | Unsure of contents | git stash show -p stash@{n}` to inspect the full diff |
Stash(20)
| Command | Level | ||
|---|---|---|---|
git stashStash the changes in a dirty working directory away | Basic | git stash | |
git stash popApply the latest stash and remove it from the stash list | Basic | git stash pop | |
git stash listList all stashes | Basic | git stash list | |
git stash applyApply a stash without removing it from the stash list | Basic | git stash apply stash@{0} | |
git stash dropRemove a single stash entry from the stash list | Basic | git stash drop stash@{0} | |
git stash showShow the changes recorded in a stash | Basic | git stash show stash@{0} | |
git stash branchCreate a new branch from a stash | Intermediate | git stash branch new-feature stash@{0} | |
git stash push -mStash changes with a description message | Basic | git stash push -m "WIP: login feature" | |
git stash push -uStash changes including untracked files | Intermediate | git stash push -u -m "work in progress" | |
git stash push -aStash all files including ignored ones | Expert | git stash push -a | |
git stash push -pInteractive stash (select specific changes) | Expert | git stash push -p | |
git stash push -kStash unstaged changes, keep staged content | Intermediate | git stash push -k | |
git stash show -pShow the full diff of a stash | Intermediate | git stash show -p stash@{0} | |
git stash clearRemove all stash entries | Intermediate | git stash clear | |
git stash apply --indexApply stash and restore staged state | Intermediate | git stash apply --index stash@{0} | |
git stash pop --indexPop stash and restore staged state | Intermediate | git stash pop --index stash@{0} | |
git stash list --formatList stash entries with custom format | Expert | git stash list --format='%C(yellow)%gd %C(reset)%s' | |
git stash push -SStash only staged changes | Expert | git stash push -S -m "stash staged only" | |
git stash -- <pathspec>Stash changes for specific paths only | Intermediate | git stash push -m "config changes" -- config/ | |
git stash saveStash changes (legacy syntax) | Basic | git stash save "WIP: refactoring auth module" |