Git Stash and Temporary Changes โ
Introduction โ
git stash lets you shelve uncommitted work so you can switch tasks, pull updates, or apply emergency fixes without committing incomplete code.
What Stash Stores โ
By default: modified tracked files + staged changes. Optionally untracked / ignored files.
Basic Usage โ
bash
git stash push -m "WIP: form validation"
git stash listExample list entry:
stash@{0}: On feature/form: WIP: form validationRestoring Work โ
bash
git stash apply stash@{0} # Keep stash copy
git stash pop # Apply then drop latestInclude Untracked Files โ
bash
git stash push -u -m "WIP: add config prototype"Include ignored files:
bash
git stash push -a -m "WIP: full env"Partial Stash (Interactive) โ
bash
git stash push -p -m "WIP: selected changes"Viewing Stash Diff โ
bash
git stash show stash@{1}
git stash show -p stash@{1} # Full patchDropping / Clearing โ
bash
git stash drop stash@{2}
git stash clear # Dangerous: removes all stashesApplying to Different Branch โ
bash
git checkout feature/new-ui
git stash apply stash@{0}Creating a Branch from a Stash โ
bash
git stash branch feature/resume stash@{0}Common Patterns โ
| Scenario | Command |
|---|---|
| Pull changes but keep WIP | git stash push -m "WIP" && git pull && git stash pop |
| Hotfix on main | Stash โ switch โ fix โ return โ pop |
| Clean workspace for build | Stash uncommitted noise |
When NOT to Use Stash โ
- Long-term storage (make a WIP commit instead)
- Sharing with others (use a branch)
- Binary-heavy work (may cause large object churn)
Alternatives โ
| Need | Alternative |
|---|---|
| Safe checkpoint | Commit to temp branch |
| Experimental spike | feature/spike-* branch |
| Quick discard | git restore |
Troubleshooting โ
| Issue | Solution |
|---|---|
| Conflict on apply | Resolve, git add, continue (stash already applied) |
| Lost stash after pop | Recover: git fsck --lost-found (sometimes) |
| Accidentally stashed untracked important file | Prefer branches in future |
Summary โ
Stash is a tactical tool for short-lived interruptions. Use it deliberately; prefer explicit commits for durable progress.
Next Steps โ
- Undo operations (
git-reset-revert-and-checkout-explained.md) - Deep conflict workflows (
git-conflict-resolution-strategies.md)
Key Commands
bash
git stash push -m "msg"
git stash list
git stash show -p stash@{n}
git stash pop
git stash branch <name> stash@{n}