What Is git stash?
A stash is a snapshot of your working directory and staging area saved onto a stack. The stack persists across branch switches and is stored in .git/refs/stash. Stashing lets you:
- Save work-in-progress without creating a messy commit
- Switch to another branch with a clean working tree
- Carry half-done changes across branches
- Store multiple independent sets of changes (named stashes)
You're building feature/login-form and have modified 4 files. Your team lead calls — there's a critical bug on main. You need a clean working tree to switch branches. Don't panic, just stash.
Basic Stash Commands
The simplest form saves all tracked changes (both staged and unstaged) and reverts your working tree to match HEAD:
# Stash all tracked changes (staged + unstaged)
git stash
# Stash with a descriptive message (highly recommended)
git stash push -m "WIP: login form validation"
# Stash including untracked files (new files not yet staged)
git stash push --include-untracked -m "WIP: login form + new helpers"
# Stash everything including ignored files
git stash push --all -m "full working tree backup"
After stashing, git status shows a clean working tree. You can now safely switch branches.
# Now you can safely switch branches
git switch main
git switch -c hotfix/null-pointer
# ... fix the bug, commit, merge ...
# Come back to your feature branch
git switch feature/login-form
Listing Your Stashes
Stashes are numbered from newest (0) to oldest. Use git stash list to see all saved stashes:
git stash list
stash@{1}: On feature/dashboard: WIP: chart component
stash@{2}: WIP on main: 3f2a1b9 Fix typo in README
You can inspect the contents of a specific stash:
# Show a summary of what's in stash@{0}
git stash show
# Show the full diff of stash@{1}
git stash show -p stash@{1}
Applying a Stash Back
There are two ways to re-apply a stash:
| Command | What it does | Stash entry afterwards |
|---|---|---|
git stash pop | Apply most recent stash | Removed from stash list |
git stash apply | Apply most recent stash | Kept in stash list |
git stash pop stash@{2} | Apply specific stash | Removed from stash list |
git stash apply stash@{1} | Apply specific stash | Kept in stash list |
# Apply the most recent stash and remove it from the stash list
git stash pop
# Apply a specific stash but KEEP it in the list (useful for applying to multiple branches)
git stash apply stash@{1}
# Create a new branch from a stash — branch out AND pop at once
git stash branch feature/continuation stash@{0}
Use git stash apply when you want to apply the same stash to multiple branches (e.g., applying a common fix to both main and a release branch). Use git stash pop for the normal flow: apply once and clean up.
Partial Stash — Choose What to Save
Sometimes you only want to stash part of your changes. The -p (patch) flag lets you interactively choose which hunks to stash:
# Interactive mode — choose which hunks to stash
git stash push -p -m "only stash the auth changes"
# Git will show each changed hunk and ask:
# y = stash this hunk
# n = keep this hunk in working tree
# s = split hunk into smaller pieces
# q = quit (stash what's been selected so far)
# ? = help
You can also stash specific files only:
# Stash only specific files
git stash push -m "WIP: auth only" -- src/auth.js src/login.js
Cleaning Up Stashes
The stash stack can grow messy over time. Prune it regularly:
# Delete a specific stash entry
git stash drop stash@{0}
# Delete ALL stashes at once (no undo!)
git stash clear
# Verify the stash list is empty
git stash list
# (no output = stash list is empty)
git stash clear deletes all stash entries and cannot be undone with a simple command. If you accidentally clear stashes, they may still be recoverable via git fsck --unreachable followed by git stash apply <dangling-blob-hash> — but only if garbage collection hasn't run yet.
📋 Summary
- git stash saves all tracked changes and reverts your working tree to HEAD — perfect for urgent context switches.
- Use git stash push -m "message" to always label your stashes for clarity.
- git stash list shows stash@{0} (newest) through stash@{N} (oldest).
- git stash pop applies the most recent stash and removes it; git stash apply stash@{N} applies and keeps it.
- git stash push -p lets you interactively pick which hunks to stash (partial stash).
- --include-untracked is needed to stash new files not yet staged.
- git stash branch <name> creates a new branch from the stash — clean way to continue work in a dedicated branch.
- git stash drop / git stash clear — clean up old stashes regularly.
FAQ
Yes. Stashes are stored in a repository-wide stack and are not tied to any specific branch. You can stash on feature/login, switch to main, and pop the stash there. However, if the stashed changes conflict with the target branch's files, you'll need to resolve merge conflicts just like a normal merge.
By default, git stash only saves changes to tracked files. Newly created files that haven't been staged with git add are untracked and are ignored by the default stash. Use git stash push --include-untracked (or -u shorthand) to include them. Use --all to also include files listed in .gitignore.
git stash pop applies the stash and immediately removes it from the stash list — it's the most common operation. git stash apply applies the stash but leaves it in the list, so you can apply it again to another branch if needed. If pop encounters a conflict, it still removes the stash entry, so you may want to use apply when conflicts are likely, giving you a chance to keep the stash as a safety net.
Yes. If the stashed changes conflict with the current branch state, Git will mark the files with conflict markers just like a merge conflict. Resolve the conflicts manually, then git add the resolved files. You do not need to run any "continue" command — the stash application is complete once you resolve the files.
No. Stashes are temporary and local — they are not pushed to remote repositories and they can be accidentally lost with git stash clear. For saving work at end-of-day or sharing work-in-progress, prefer a WIP: commit on your feature branch that you can amend or squash later. Stashes are best for very short context switches (minutes to hours).