Half an hour into a change, a bug report arrives that needs a clean branch right now. git stash exists for that moment. It is also one of the easier ways to lose work in Git, because its defaults leave some files behind, quietly drop staging, and keep entries in a list nobody looks at. Each behavior below was tested in a scratch repository with Git 2.43.0, and the output shown is what Git printed.
What git stash does
A clean working directory, on demand
The Git documentation describes the purpose plainly: use git stash when the goal is to record the current state of the working directory and the index but return to a clean working directory. The command saves local modifications and reverts the working directory to match the HEAD commit.
In the test repository, a.txt had an uncommitted edit. Running git stash printed:
Saved working directory and index state WIP on main: 5a502d9 init
The edit to a.txt disappeared from the working tree. The message names the branch and the commit the stash was made on, which becomes useful later when the list gets long.
Where stashes live
git stash list shows saved entries, newest first, numbered from zero:
stash@{0}: WIP on main: 5a502d9 init
A stash is stored as commits under a special reference, not as a loose file. That detail is what makes recovery possible after a mistake, as the last section shows.

The file it did not take
Untracked files stay put
The same test repository also had a brand-new file, new.txt, that had never been added. After git stash, git status --short still showed it:
?? new.txt
Plain git stash saves changes to tracked files only. A new file that Git has never seen is not part of the stash. If the next step is switching branches, that file travels along into the other branch’s working tree, which is rarely what anyone wanted.
Including untracked files
The -u or --include-untracked option fixes that. The documentation says that with this option all untracked files are also stashed and then cleaned up with git clean. Run as git stash push -u -m "with untracked", the working tree came back fully clean, and the list showed the message:
stash@{0}: On main: with untracked
The -m message is worth the extra second. “WIP on main” repeated five times in a list tells nobody anything.
Stashing only part of the work
By path
A stash does not have to take everything. Files named after -- limit it: the documentation says the new entry records the modified states only for the files that match the pathspec, and rolls back only those files. With edits to both a.txt and b.txt, git stash push -m "only a" -- a.txt saved the first and left the second in place:
M b.txt
stash@{0}: On main: only a
Only what is staged
--staged, available in recent Git versions, stashes only the changes already added to the index. The documentation compares it to a basic git commit, except the state goes to the stash instead of the current branch. In the test, with a.txt staged and b.txt merely modified, the stash took a.txt alone and b.txt stayed in the working tree.
Keeping the index
--keep-index works the other way round, and its behavior surprised the test. The documentation says all changes already added to the index are left intact. They are, but they are also saved: with a.txt staged and b.txt modified, git stash push --keep-index left a.txt staged in the working tree, while git stash show --stat showed the stash holding both files. The common use is to test exactly what is about to be committed, with every unstaged edit set aside, and then pop the rest back.
Why untracked files matter when switching branches
An untracked file left behind by a plain stash can do more than clutter the other branch. In the test, a branch called other had committed a file named d.txt. Back on main, an untracked local file with the same name blocked the switch outright:
error: The following untracked working tree files would be overwritten by checkout:
d.txt
A plain git stash would not have cleared that path. git stash -u would have.

Looking inside a stash
show and show -p
git stash show summarizes which files changed in an entry; git stash show -p prints the full diff. With --include-untracked, show also includes untracked files in the diff, per the documentation. On the -u stash it printed both the edit to a.txt and the whole content of new.txt as a new file:
diff --git a/a.txt b/a.txt
@@ -1 +1,2 @@
line1
+edit
diff --git a/new.txt b/new.txt
new file mode 100644
+new
Reading a stash before applying it is the cheap way to avoid applying the wrong one.
pop, apply, and the staging area
pop versus apply
git stash pop applies the newest entry and removes it from the list. git stash apply applies it and keeps it. The difference matters when the same changes need to land on more than one branch, or when the outcome is uncertain: apply leaves a copy until a deliberate drop.
Staged changes come back unstaged
The quieter surprise is the index. In the test, a.txt was staged with git add, then stashed, then popped. It came back as a modified file, but no longer staged:
M a.txt
The leading space in M means “modified, not staged”. The staging decision had been lost.
The --index option restores it. The documentation says it tries to reinstate not only the working tree’s changes, but also the index’s. Repeating the test with git stash pop --index brought the file back staged:
M a.txt
For anyone who builds commits carefully with git add -p, --index should be habit.

When pop hits a conflict
The stash is kept
If the branch has changed under a stash, applying it can conflict like any merge. The test committed a different edit to the same line of a.txt, then ran git stash pop:
CONFLICT (content): Merge conflict in a.txt
The stash entry is kept in case you need it again.
The exit code was 1, git status --short showed UU a.txt, and git stash list still held the entry. The documentation describes exactly this: applying the state can fail with conflicts, in which case the entry is not removed from the list, and the conflicts must be resolved by hand followed by a manual git stash drop.
That last step is the one people forget. A conflicted pop turns into an apply, and the entry sits in the list until someone drops it. After the conflict was resolved in the test, git stash drop printed Dropped refs/stash@{0} with the entry’s hash, and the list was empty.
git stash branch
When a stash is old enough that the code around it has moved, git stash branch sidesteps the conflict. It creates and checks out a new branch starting at the commit where the stash was originally made, applies the stash there, and drops it if that succeeds. In the test, git stash branch fix-x switched to fix-x, applied the changes cleanly and printed the Dropped line. The stashed work ends up on its own branch, based on the code it was written against, ready to be merged or rebased like any other branch.
Getting back a dropped stash
Not covered by the reflog
A drop or clear is the one stash operation that looks final. The documentation notes that cleared entries become subject to pruning and may be impossible to recover; in the test, git stash clear took the list from one entry to none with no confirmation and no output. The documentation warns that dropped or cleared entries cannot be recovered through the normal safety mechanisms, and then gives a way to try anyway: a git fsck --unreachable pipeline that lists unreachable commits, which still includes stash commits until garbage collection removes them.
The test
The test stashed a change, noted the hash from git stash list --format=%H, and dropped it. git fsck --unreachable | grep commit then listed several unreachable commits, among them the stash and the internal commits Git creates alongside it. Applying the noted hash directly worked:
$ git stash apply <hash>
$ git status --short
M a.txt
?? new.txt
Two lessons follow. The Dropped refs/stash@{0} (...) line that drop and pop print contains the full hash; copying it out of the terminal scrollback is the fastest recovery there is. And the window is limited: once Git’s garbage collection prunes unreachable objects, the work is gone.
Keeping the list short
None of this is a reason to avoid stash. It is a reason to treat it as a short-term shelf. A stash older than a day or two is usually better as a branch, with a real commit message, where it shows up in git log and cannot be dropped by accident. Files that should never be committed or stashed at all belong in .gitignore, covered in the earlier post on .gitignore patterns.
Sources
- git-stash documentation (git-scm.com): https://git-scm.com/docs/git-stash
