Browse cheat sheets · Git
Git Stash
Use Git stash safely to park unfinished work, switch context, inspect and restore stashed changes, handle untracked files, and recover when stash application conflicts.
On this page
- Problem
- Quick answer
- Code / commands
- 1. Check the working tree first
- 2. Stash tracked changes
- 3. Include untracked files
- 4. Understand what -u does not include
- 5. Give every stash a useful message
- 6. List stashes
- 7. Show stash summary
- 8. Inspect full stash patch
- 9. Apply a stash without deleting it
- 10. Apply the latest stash
- 11. Pop the latest stash
- 12. Pop a specific stash
- 13. Prefer apply for valuable work
- 14. Drop one stash
- 15. Clear all stashes only when intentional
- 16. Stash only selected files
- 17. Stash interactively by hunk
- 18. Keep staged changes in place
- 19. Stash only staged changes
- 20. See the current Git version
- 21. Create a branch directly from a stash
- 22. Use stash branch when context changed significantly
- 23. Stash before switching branches
- 24. Stash before rebasing
- 25. Use rebase autostash when appropriate
- 26. Stash before pulling when local edits block the update
- 27. Inspect status after applying
- 28. Handle stash apply conflicts
- 29. Understand apply conflict behavior
- 30. Understand pop conflict behavior
- 31. Check for conflicts after pop
- 32. Restore staged state from a stash
- 33. Pop while restoring the index
- 34. Inspect the stash as commits
- 35. Compare a stash with current work
- 36. Compare stash contents with its original base
- 37. Stash a partially staged file carefully
- 38. Use patch mode for precise stash scope
- 39. Use pathspecs to avoid stashing unrelated files
- 40. Do not use stash as long-term project storage
- 41. Convert important stash work into a branch
- 42. Use a WIP commit instead when collaboration matters
- 43. Remember stashes are local by default
- 44. Use stash for short context switches
- 45. Recover a recently dropped stash with reflog/object inspection
- 46. Do not run aggressive garbage collection during recovery
- 47. Preview clean state after stashing
- 48. Stash does not replace .gitignore
- 49. Stash does not solve branch design problems
- 50. Verify before dropping
- What the result means
- What to look for
- New files disappeared after stashing
- Stash applies with conflicts
- pop did not remove the stash
- Staged/unstaged split changed after apply
- You have many old stashes
- Production notes
- Stashes are local state
- Apply is safer than pop for important work
- Untracked and ignored files require explicit options
- Stash conflicts are normal after branch divergence
- Stashes can contain sensitive files
- Long-lived work belongs on a branch
- Common mistakes
- 1. Assuming untracked files are stashed automatically
- 2. Using pop before inspecting an old stash
- 3. Clearing all stashes casually
- 4. Treating stash as durable backup
- 5. Stashing ignored files with -a without checking them
- 6. Assuming staged state is always restored exactly
- 7. Dropping a stash before testing restored work
- 8. Leaving stashes unnamed
- 9. Applying a very old stash onto a heavily changed branch
- 10. Using stash to manage several long-running tasks
- Next step
- Related deep dives
Problem
You have unfinished local work but need to:
- switch branches
- pull or rebase
- test another change
- handle an urgent fix
- clean the working tree temporarily
- preserve edits without creating a permanent commit
git stash can help, but careless use causes problems:
- forgetting what a stash contains
- losing track of multiple stashes
- assuming untracked files are included automatically
- using
popbefore inspecting - creating conflicts when restoring
- treating stash as long-term storage
- clearing stashes accidentally
The goal is to use stash as a temporary, inspectable safety tool, not as a substitute for branches or commits.
Quick answer
Stash tracked changes:
git stash push -m "WIP report engine"
Include untracked files:
git stash push -u -m "WIP report engine"
List stashes:
git stash list
Inspect before restoring:
git stash show -p stash@{0}
Apply without deleting:
git stash apply stash@{0}
Remove only after confirming the work is restored:
git stash drop stash@{0}
Use pop only when you are comfortable applying and removing the stash in one step:
git stash pop
Code / commands
1. Check the working tree first
git status
Know what will be stashed before running the command.
A stash can include different sets of changes depending on options.
2. Stash tracked changes
git stash push -m "WIP report engine"
This stashes tracked staged and unstaged changes by default.
The working tree is returned toward the state of HEAD.
3. Include untracked files
git stash push -u -m "WIP report engine"
-u / --include-untracked includes untracked files.
Use this when new files are part of the unfinished work.
4. Understand what -u does not include
Ignored files are not included by -u.
If you truly need ignored files too:
git stash push -a -m "WIP including ignored files"
-a / --all includes ignored files.
Use carefully because ignored files often include:
- build artifacts
- caches
- local environment files
- generated data
5. Give every stash a useful message
Prefer:
git stash push -m "WIP Fabric pipeline retry changes"
over:
git stash
Meaningful messages matter once several stashes exist.
6. List stashes
git stash list
Example:
stash@{0}: On feature/report-engine: WIP retry logic
stash@{1}: On main: WIP README update
The newest stash is usually stash@{0}.
7. Show stash summary
git stash show stash@{0}
This shows a diffstat-style summary.
8. Inspect full stash patch
git stash show -p stash@{0}
Do this before restoring an older or unfamiliar stash.
9. Apply a stash without deleting it
git stash apply stash@{0}
Use this when you want to keep the stash until you verify the restored work.
This is safer than pop for important changes.
10. Apply the latest stash
git stash apply
Equivalent to applying the latest stash.
When multiple stashes exist, specifying the exact stash is clearer.
11. Pop the latest stash
git stash pop
This applies the latest stash and, if application succeeds cleanly, removes that stash entry.
Use when you are confident the restore is straightforward.
12. Pop a specific stash
git stash pop stash@{1}
Useful when the desired stash is not the latest one.
13. Prefer apply for valuable work
Safer recovery flow:
git stash apply stash@{0}
git status
git diff
After verification:
git stash drop stash@{0}
This keeps a backup until you confirm the work is restored correctly.
14. Drop one stash
git stash drop stash@{0}
This deletes that stash reference.
Inspect it first if there is any doubt.
15. Clear all stashes only when intentional
git stash clear
This removes all stash entries.
Do not use it as routine cleanup.
There is no normal per-entry confirmation.
16. Stash only selected files
git stash push \
-m "WIP API changes" \
-- src/api.py src/config.py
This leaves unrelated working-tree changes in place.
Use pathspecs when only part of the work needs to move aside.
17. Stash interactively by hunk
git stash push -p
Git prompts for each hunk.
Use this when one file contains:
- work you want to stash
- work you want to keep active
18. Keep staged changes in place
If supported by your Git version:
git stash push --keep-index
This stashes other changes while leaving staged changes in the index.
Useful when you want to test the staged commit independently.
19. Stash only staged changes
On modern Git versions:
git stash push --staged
This stashes the staged content and leaves unstaged edits in the working tree.
Check your Git version if this option is unavailable.
20. See the current Git version
git --version
Useful when stash options differ across older Git installations.
21. Create a branch directly from a stash
git stash branch recovery-work stash@{0}
This:
- creates a new branch from the commit where the stash was originally created
- applies the stash
- drops it if application succeeds
Use this when the current branch has diverged and direct stash application produces conflicts.
22. Use stash branch when context changed significantly
Example situation:
stash created two weeks ago
main changed heavily
feature branch changed heavily
Instead of applying onto today’s branch:
git stash branch old-wip stash@{2}
This often gives Git a more compatible base.
23. Stash before switching branches
git stash push -u -m "WIP before hotfix"
git switch hotfix/production
After the hotfix:
git switch feature/report-engine
git stash apply stash@{0}
Use explicit messages so the stash remains easy to identify.
24. Stash before rebasing
git stash push -u -m "WIP before rebase"
git fetch origin
git rebase origin/main
git stash apply stash@{0}
A clean working tree makes rebase easier to reason about.
25. Use rebase autostash when appropriate
git rebase --autostash origin/main
Git creates a temporary stash automatically and reapplies it at the end.
This is convenient, but conflicts can still occur when the temporary stash is restored.
Manual stash is often easier to inspect and control.
26. Stash before pulling when local edits block the update
If the team workflow requires pull/rebase:
git stash push -u -m "WIP before sync"
git pull --ff-only
or:
git fetch origin
git rebase origin/main
Then restore the stash.
Do not stash automatically without checking whether those edits should actually be committed first.
27. Inspect status after applying
git status
Then:
git diff
git diff --cached
Do not assume stash application restored exactly what you expected.
28. Handle stash apply conflicts
If application conflicts:
git status
Git marks conflicted files.
Resolve them manually, then:
git add path/to/file
Unlike rebase, there is no:
git stash --continue
You resolve the working tree normally.
29. Understand apply conflict behavior
If:
git stash apply stash@{0}
conflicts, the stash entry remains.
That is useful because the original stash still exists while you resolve the working tree.
30. Understand pop conflict behavior
If git stash pop encounters conflicts, Git generally keeps the stash instead of dropping it.
Still verify with:
git stash list
Do not assume the stash was removed.
31. Check for conflicts after pop
git status
Then:
git stash list
Confirm both the working-tree state and whether the stash entry still exists.
32. Restore staged state from a stash
By default, applying a stash restores file changes but may not recreate the exact staged/unstaged split.
Use:
git stash apply --index stash@{0}
to attempt to restore staged state too.
This can fail if the index cannot be applied cleanly.
33. Pop while restoring the index
git stash pop --index
Use only when restoring the original staging state matters.
For valuable work, prefer apply --index first.
34. Inspect the stash as commits
A stash is represented internally using Git commits.
You can inspect it:
git show stash@{0}
or:
git log -g --oneline refs/stash
You normally do not need the internal structure, but knowing the stash is backed by Git objects helps explain why reflog-based recovery may sometimes work.
35. Compare a stash with current work
git diff stash@{0}
For a specific file:
git diff stash@{0} -- src/app.py
Use this before applying if current code has moved significantly.
36. Compare stash contents with its original base
git stash show -p stash@{0}
This is usually the clearest way to see what the stash introduced.
37. Stash a partially staged file carefully
A file can contain:
staged changes
+
unstaged changes
Before stashing, inspect both:
git diff --cached
git diff
Then decide whether you need:
- normal stash
--keep-index--staged- patch mode
Do not assume the staged boundary will behave the way you intend.
38. Use patch mode for precise stash scope
git stash push -p -m "Only experimental changes"
Use this when you need hunk-level control.
Review each hunk carefully.
39. Use pathspecs to avoid stashing unrelated files
git stash push \
-m "WIP backend only" \
-- src/api src/services
This is useful in repositories where several unrelated tasks are being worked on at once.
Better yet, consider separate branches/worktrees if that pattern is frequent.
40. Do not use stash as long-term project storage
Bad pattern:
stash@{0} 3 months old
stash@{1} 4 months old
stash@{2} "WIP"
stash@{3} "stuff"
If the work matters, prefer:
temporary branch
+
normal commits
Branches are easier to:
- review
- push
- back up
- share
- recover
41. Convert important stash work into a branch
git stash branch feature/recovered-work stash@{0}
Then commit normally.
This turns temporary state into durable history.
42. Use a WIP commit instead when collaboration matters
Instead of stashing important work:
git add -A
git commit -m "WIP: report engine changes"
on a private feature branch can be safer because the work can be pushed remotely.
Later clean history with interactive rebase if needed.
Do not push WIP commits to protected/shared branches unless team policy allows it.
43. Remember stashes are local by default
A stash is not automatically pushed to GitHub, GitLab, Azure DevOps, or another remote.
If your laptop disappears, an unpushed stash may disappear with it.
Important work should become a commit on a branch.
44. Use stash for short context switches
Good examples:
urgent hotfix
quick branch comparison
temporary rebase cleanup
test clean working tree
Poor use:
store a week's unfinished feature indefinitely
45. Recover a recently dropped stash with reflog/object inspection
Start with:
git reflog --all
and:
git fsck --no-reflogs --unreachable
Recovery is not guaranteed and becomes more difficult after garbage collection.
Do not depend on this as a normal workflow.
Prevention is better: inspect before dropping and use branches for important work.
46. Do not run aggressive garbage collection during recovery
If you are trying to recover a dropped stash, avoid commands that may prune unreachable objects.
Create a recovery reference as soon as you identify the relevant commit.
47. Preview clean state after stashing
After:
git stash push -u -m "WIP"
run:
git status
Confirm the working tree is actually clean enough for the next operation.
Ignored files may still remain.
48. Stash does not replace .gitignore
If generated files repeatedly interfere with branch operations, fix the repository ignore rules where appropriate.
Do not repeatedly stash or clean files that should never be version-controlled.
49. Stash does not solve branch design problems
If you constantly stash because you have several parallel tasks in one working directory, consider:
- separate feature branches
git worktree- smaller task boundaries
Stash is for temporary displacement, not continuous multitasking architecture.
50. Verify before dropping
A safe sequence:
git stash apply stash@{0}
git status
git diff
# run tests if relevant
git stash drop stash@{0}
Do not delete the backup until the restored state is known to be correct.
What the result means
| Command | What it does |
|---|---|
git stash push |
Saves tracked staged/unstaged changes temporarily |
git stash push -u |
Also includes untracked files |
git stash push -a |
Also includes ignored files |
git stash list |
Lists stash entries |
git stash show -p |
Shows stash patch |
git stash apply |
Restores stash but keeps stash entry |
git stash pop |
Restores and normally removes stash on success |
git stash apply --index |
Attempts to restore staged state too |
git stash drop |
Deletes one stash entry |
git stash clear |
Deletes all stash entries |
git stash branch |
Creates branch from stash’s original base and applies it |
The safest restore pattern is:
inspect
→ apply
→ verify
→ drop
What to look for
New files disappeared after stashing
You probably used:
git stash push
without:
-u
Untracked files are not included by default.
Stash applies with conflicts
The target branch changed since the stash was created.
Use:
git status
resolve manually, or consider:
git stash branch
from the stash’s original base.
pop did not remove the stash
Check:
git stash list
A conflict may have caused Git to retain it.
Staged/unstaged split changed after apply
Use:
git stash apply --index
when preserving the index state matters.
You have many old stashes
Important work is being stored in the wrong place.
Convert useful stashes into branches/commits and remove obsolete ones deliberately.
Production notes
Stashes are local state
They are not a replacement for remote backup.
If the work matters, commit it to a branch and push when appropriate.
Apply is safer than pop for important work
apply leaves the stash available until verification.
This gives you a fallback if conflict resolution or restored state is wrong.
Untracked and ignored files require explicit options
Use:
-u
for untracked files.
Use:
-a
only when ignored files truly need to be included.
Stash conflicts are normal after branch divergence
A stash is a patch-like snapshot against an earlier repository state.
The longer it sits, the more likely application conflicts become.
Stashes can contain sensitive files
Using -a may stash ignored local configuration or secret-bearing files.
They remain local Git objects.
Avoid including secrets unnecessarily.
Long-lived work belongs on a branch
Branches and commits provide clearer recovery, history, CI integration, and remote backup.
Common mistakes
1. Assuming untracked files are stashed automatically
Use -u when new files must be included.
2. Using pop before inspecting an old stash
Prefer show + apply for important work.
3. Clearing all stashes casually
git stash clear removes every stash entry.
4. Treating stash as durable backup
Stashes are local and easy to forget or delete.
5. Stashing ignored files with -a without checking them
You may include local secrets or huge generated directories.
6. Assuming staged state is always restored exactly
Use --index when that matters.
7. Dropping a stash before testing restored work
Keep the backup until verification is complete.
8. Leaving stashes unnamed
Multiple entries named “WIP” become difficult to distinguish.
9. Applying a very old stash onto a heavily changed branch
Use git stash branch if direct application is conflict-heavy.
10. Using stash to manage several long-running tasks
Use branches or worktrees instead.
Was this helpful?
What could be better?
Thanks — that helps me improve Javi Builds.
Stay in the loop
Get the Javi Builds updates you actually care about.
Check your inbox
I sent you a confirmation link. Your subscription will be active after you confirm it.