Git stash Guide
When to Use
When you're mid-task and need to temporarily switch branches, handle an urgent issue, or simply don't want to commit a batch of half-finished changes, you can stash the current state.
Check Current State First
git status --short
git stash list
git status --short-- confirm what changes currently exist.git stash list-- confirm what stashes are already saved in the repository.
Recommended Commands
| Scenario | Recommended Command | Result |
|---|---|---|
| Stash tracked changes | git stash push -m "wip: message" | Save current state and restore clean working directory |
| Stash including untracked files | git stash push -u -m "wip: message" | Also includes new files |
| View stash list | git stash list | See all saved stashes |
| View what a stash changed | git stash show -p stash@{0} | Show the diff |
| Restore and delete stash | git stash pop | Auto-removed after successful application |
| Restore but keep stash | git stash apply stash@{0} | Convenient for repeated attempts |
| Delete a stash | git stash drop stash@{0} | Manually clean up unused stashes |
Common Scenarios
Temporarily Switch to Another Branch
git stash push -m "wip: refactor auth"
git switch main
Restore when you come back:
git switch feature/auth
git stash pop
Stash Untracked Files Too
git stash push -u -m "wip: include new config"
By default, git stash does not include untracked files; use -u to include them.
Check What's in the Stash Before Restoring
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
Create a New Branch Directly from a Stash
git stash branch rescue-work stash@{0}
Useful when the stash and current branch have diverged significantly and you don't want to restore directly on the original branch.
Risks and Boundaries
git stashonly saves changes to tracked files by default; ignored files require-a, untracked files require-u.git stash poponly deletes the stash when application succeeds; if a conflict occurs, the stash is usually preserved.- Stashes live in the local repository and are not automatically synced to the remote.
- If the changes are stable enough, creating a branch or making a regular commit is usually cleaner than accumulating stashes long-term.
Related Commands
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
git stash drop stash@{0}