git stash
Stash the changes in a dirty working directory away
The 'git stash' command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. You can re-apply the stashed changes later with 'git stash pop' or 'git stash apply'. Stashing is useful when you need to quickly switch context and work on something else, but you're mid-way through a change and aren't ready to commit. The stash is stored as a stack, so you can have multiple stashes.
Synopsis
git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply [--index] [-q | --quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet] [-u | --include-untracked] [-a | --all] [(-m | --message) <message>] [--] [<pathspec>...]]
git stash clearOptions
Choosing the Right Option
Restoring Stashed Changes
Choose the right command to restore your stashed work
| Command | Keeps Stash? | Applies To | On Conflict |
|---|---|---|---|
git stash pop | No - removes after apply | Current branch | Stash kept, must resolve |
git stash apply | Yes - stays in stack | Current branch | Stash kept, must resolve |
git stash branch <name> | No - removes after apply | New branch at stash base | No conflicts (fresh branch) |
What Gets Stashed?
Control which files are included in your stash
| Command | Tracked Modified | Staged | Untracked | Ignored |
|---|---|---|---|---|
git stash | Yes | Yes | No | No |
git stash -u | Yes | Yes | Yes | No |
git stash -a | Yes | Yes | Yes | Yes |
git stash -k | Yes | Yes (and also kept staged) | No | No |
Stash vs Other Save Methods
When to use stash versus alternatives
| Command | Persists Where | Shareable | Best For |
|---|---|---|---|
git stash | Local reflog | No | Quick context switch, temporary save |
git commit (WIP) | Branch history | Yes (if pushed) | Work you might want to share or reference |
git worktree | Separate directory | N/A | Working on multiple branches simultaneously |
Managing Multiple Stashes
Commands for working with the stash stack
| Command | Shows Content | Modifies Stack | Use Case |
|---|---|---|---|
git stash list | Index + message | No | See all stashes |
git stash show | Diffstat | No | Quick preview of changes |
git stash show -p | Full diff | No | Detailed review before apply |
git stash drop | No | Yes (removes one) | Delete specific stash |
git stash clear | No | Yes (removes ALL) | Start fresh (dangerous) |
Examples
Stash current changes
git stashSaves all uncommitted changes (staged and unstaged) to the stash stack and reverts working directory to HEAD.
Stash with a descriptive message
git stash push -m "WIP: login feature"Stashes changes with a custom message for easier identification when you have multiple stashes.
Apply and remove latest stash
git stash popApplies the most recent stash (stash@{0}) to your working directory and removes it from the stash stack.
List all stashes
git stash listShows all stash entries with their index, branch name, and message. Useful when managing multiple stashes.
Workflow Examples
git stashSave current changes to stash
git checkout other-branchSwitch to the other branch
(do work, commit if needed)Complete the urgent work
git checkout original-branchReturn to your branch
git stash popRestore your stashed changes
git stash push -m "WIP: login validation"Stash with descriptive message
(days later...)Time passes
git stash listSee all stashes with messages
git stash show stash@{0}Preview what's in the stash
git stash popApply and remove when ready
git stashStash your changes
git checkout branch-aSwitch to first branch
git stash applyApply (keeps stash)
git commit -m "Add feature"Commit on branch-a
git checkout branch-bSwitch to second branch
git stash applyApply same stash again
git stash dropClean up when done
git add ready-file.jsStage the ready changes
git stash -kStash unstaged, keep staged
git commit -m "Ready feature"Commit the staged changes
git stash popRestore the rest of your work
Troubleshooting
error: Your local changes to the following files would be overwritten by merge
Cause: Trying to pop/apply a stash that conflicts with current working directory
Either commit/stash current changes first, or use 'git stash branch <name>' to create a new branch from the stash
No stash entries found
Cause: The stash stack is empty or you're looking at the wrong index
Run 'git stash list' to see available stashes. Remember stash@{0} is most recent.
Stash didn't save my new files
Cause: By default, git stash only saves tracked files
Use 'git stash -u' to include untracked files, or 'git stash -a' to include ignored files too
Applied wrong stash / want to undo pop
Cause: Accidentally popped the wrong stash entry
If pop failed with conflicts, stash is still there. If successful, your changes are in working directory - just 'git stash' again if needed
Lost stash after clear/drop
Cause: git stash clear/drop permanently removes stash entries
Try 'git fsck --unreachable | grep commit' then 'git show <hash>' to find lost stash commits. Recovery not guaranteed.
Tips & Pitfalls
Best Practices
- ✓Always use descriptive messages with 'git stash push -m "description"' - you'll thank yourself later
- ✓Prefer 'git stash apply' over 'git stash pop' when you might need the stash again
- ✓Use 'git stash -u' if you have new untracked files you want to save
- ✓Don't let stashes pile up - regularly clean old stashes with 'git stash drop'
- ✓Consider creating a WIP commit instead of stashing for long-term saves
- ✓Use 'git stash show -p' to review stash contents before applying
- ✓When stash pop fails due to conflicts, the stash is preserved - resolve conflicts then drop manually
Common Mistakes
- ✗Forgetting that 'git stash' only stores tracked files by default. Use '-u' or '--include-untracked' to stash new untracked files.
- ✗Using 'git stash pop' when you might want to apply the same stash to multiple branches. Use 'git stash apply' instead to keep the stash.
- ✗Not giving stashes descriptive messages and then forgetting what each stash contains. Always use 'git stash push -m "description"'.
- ✗Running 'git stash clear' without realizing it deletes ALL stashes without confirmation. Recovery is only possible by digging dangling commits out with 'git fsck'.
- ✗Confusing the stash stack order: stash@{0} is the MOST recent stash, not the oldest.
FAQ
Pop applies the stash AND removes it from the stack. Apply only applies it, keeping the stash available. Use apply when you might need the same stash elsewhere.
By default, stash only saves tracked files. Use 'git stash -u' to include untracked files, or 'git stash -a' to also include ignored files.
Use 'git stash show' for a quick summary, or 'git stash show -p' for the full diff. Add stash@{n} to view a specific stash.
Yes! Use 'git stash push -m "message" -- file1.js file2.js' to stash only specific files, or 'git stash -p' to interactively select hunks.
The stash is NOT removed when conflicts occur. Resolve the conflicts manually, then use 'git stash drop' to remove the stash when done.
Stashes are branch-independent. Just checkout the target branch and run 'git stash apply'. Or use 'git stash branch <name>' to create a new branch.