Git Stash Tutorial - stash, pop vs apply, -u for Untracked Files, stash branch, and What Happens When git stash pop Hits a Conflict (Real Output from git 2.43.0)

TL;DR

  • git stash saves your uncommitted changes to a stack and resets the working tree to HEAD. git stash push -m "message" is the form I use, because the message is what you read later in git stash list. The git stash reference marks the older git stash save as deprecated in favour of git stash push.
  • pop = apply + drop. apply = apply and keep the entry. If the apply hits a conflict, pop keeps the entry on the stack and exits with code 1 (section 6 shows it).
  • Untracked files are not stashed by default. Add -u (--include-untracked); -a (--all) also takes ignored files.
  • Staged state is lost on pop unless you pass --index. I show M versus M in section 3.
  • Stash one file: git stash push -m "msg" -- path/to/file. The pathspec only works after --.
  • git stash branch <name> creates a branch at the commit where you stashed, applies the stash and drops it, so it cannot conflict with whatever main did meanwhile.
  • Not your case? To keep the work as a real commit on a branch read git branching explained; to throw away or move commits instead of uncommitted edits read git reset soft, mixed and hard.

Table of Content

  1. What git stash actually does
  2. Stash with a message and list it
  3. pop vs apply vs pop --index
  4. Untracked files with -u
  5. Stash one file only
  6. When git stash pop hits a conflict
  7. stash branch
  8. Drop, clear and recovering a dropped stash
  9. What broke when I ran this
  10. Conclusion

1. What git stash actually does

You are halfway through a change, a colleague asks you to look at another branch, and git switch complains that your local changes would be overwritten. You do not want a half-finished commit. git stash records the modified tracked files (and the index) as a special commit, stores it in refs/stash, and resets the working tree to HEAD. Every stash is a stack entry: stash@{0} is the newest, stash@{1} the one before it. The reference page says the entries use the usual reflog syntax, which is why they behave like reflog entries and why a dropped stash can be recovered for a while (section 8).

git stash moves changes between working tree, stash stack and a clean HEAD

Here are the subcommands I use, all verified against the reference page:

CommandWhat it doesEntry kept afterwards?
git stash push -m "msg"Saves tracked changes, resets working treeNew entry added
git stash listLists entries as stash@{n}: On branch: messagen/a
git stash show -p stash@{n}Shows the entry as a patchYes
git stash apply [stash@{n}]Re-applies the changesYes
git stash pop [stash@{n}]Applies, then drops the entry if it applied cleanlyNo (kept on conflict)
git stash pop --indexAlso restores what was stagedNo
git stash push -uIncludes untracked filesNew entry added
git stash push -aIncludes untracked and ignored filesNew entry added
git stash branch name [stash]New branch at the stash's base commit, apply, dropNo
git stash drop [stash]Deletes one entryn/a
git stash clearDeletes all entriesn/a

All the outputs below come from git 2.43.0, run on 2026-12-03 in a throwaway repository with one commit, a three-line app.txt and a one-line notes.txt. If you have not used Git before, start with what is git.

2. Stash with a message and list it

I append a line to app.txt and stash it with a message:

1$ echo "header change" >> app.txt; git stash push -m "wip header"
2Saved working directory and index state On main: wip header
3$ git stash list
4stash@{0}: On main: wip header
5$ git status --short   (nothing printed = clean)

Without -m the entry is labelled WIP on main: <hash> <subject>, which is useless once you have three of them. The sample in the reference page looks the same, stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation. I always type the message.

3. pop vs apply vs pop --index

pop and apply both re-apply the stash; pop then deletes the entry. The case that surprises people is the index. I staged a change with git add, stashed, and popped it twice, once plain and once with --index:

1$ git stash pop   (staged state lost)
2 M app.txt
3$ git stash pop --index   (staged state kept)
4M  app.txt

The first column of git status --short is the index and the second the working tree. A plain pop brings the edit back as unstaged ( M); --index restores it as staged (M ). The reference says --index "tries to reinstate not only the working tree's changes, but also the index's ones", and it can fail when you have conflicts. I use apply when I am unsure the stash applies cleanly, because it leaves the safety copy on the stack, and pop once I know it does.

4. Untracked files with -u

A new file that was never added is invisible to a plain stash. I created scratch.txt and modified app.txt:

 1$ git stash   (scratch.txt stays behind)
 2?? scratch.txt
 3 M app.txt
 4?? scratch.txt
 5$ git stash push -u -m "with untracked"
 6Saved working directory and index state On main: with untracked
 70
 8$ git stash show --include-untracked --stat
 9 app.txt     | 1 +
10 scratch.txt | 1 +
11 2 files changed, 2 insertions(+)

After the plain git stash the working tree still held ?? scratch.txt. I popped it back (the second status shows both files again), then ran git stash push -u; the 0 is git status --short | wc -l, so the tree was completely clean, and the stat shows both files in one entry. -a goes one step further and takes ignored files too; the reference says they are "cleaned up with git clean", so use it sparingly, because build output matching your .gitignore goes into the stash and is deleted from disk until you pop.

5. Stash one file only

Two files are modified and I only want to park notes.txt:

1$ git stash push -m "only notes" -- notes.txt
2Saved working directory and index state On main: only notes
3$ git status --short
4 M app.txt
5stash@{0}: On main: only notes

app.txt stays modified, notes.txt is parked. Per the reference, pathspec elements are only allowed after --, and git stash save cannot take a pathspec at all. For picking individual hunks, git stash push -p is interactive and implies --keep-index.

6. When git stash pop hits a conflict

I stashed an edit to the last line of app.txt, committed a different edit to the same line on main, then popped:

 1$ git stash pop
 2Auto-merging app.txt
 3CONFLICT (content): Merge conflict in app.txt
 4On branch main
 5Unmerged paths:
 6  (use "git restore --staged <file>..." to unstage)
 7  (use "git add <file>..." to mark resolution)
 8	both modified:   app.txt
 9
10no changes added to commit (use "git add" and/or "git commit -a")
11The stash entry is kept in case you need it again.
12exit code 1
13$ git stash list
14stash@{0}: On main: conflict demo

The file then contains the usual markers, with the stash labelled as the second side:

1line1
2line2
3line3
4<<<<<<< Updated upstream
5committed version
6=======
7stash version
8>>>>>>> Stashed changes

The important line is The stash entry is kept in case you need it again. pop did not drop anything, matching the reference: when applying "can fail with conflicts", the entry stays and you must resolve by hand. My routine is: edit the file to the version I want, git add app.txt, then git stash drop myself:

1$ git stash drop
2Dropped refs/stash@{0} (35e11d8661e680379f585236cc3e2415434eca8a)

The hash in parentheses matters, see section 8. If you would rather abort, git restore --staged app.txt and git restore app.txt undo the half-applied state and the stash is still on the stack (git restore).

7. stash branch

If the branch has moved on so much that a pop conflicts, git stash branch avoids the conflict by construction. I stashed a change to notes.txt, committed a new line to app.txt on main, and ran:

1$ git stash branch feature-x
2Switched to a new branch 'feature-x'
3On branch feature-x
4Changes not staged for commit:

The reference describes it as creating the branch "starting from the commit at which the stash was originally created", applying the changes, and dropping the stash if it was a stash@{n} reference. Because it is applied on the exact commit it was taken from, "it restores the originally stashed state with no conflicts". I think it is the most under-used stash command; it turns a parked change into a normal feature branch in one step. For what to do with that branch next, see git branching explained and the git switch reference.

8. Drop, clear and recovering a dropped stash

git stash drop stash@{1} removes one entry; git stash clear removes all of them, and the reference warns they "may be impossible to recover" afterwards because they become subject to pruning. A single dropped entry is different: the drop output prints its hash. I stashed an edit, dropped it, and applied the hash:

1$ git stash apply <hash from drop output>
2 M app.txt

That works because the stash commit is still in the object database until garbage collection prunes it. The git gc reference gives the defaults: gc.pruneExpire prunes loose objects older than 2 weeks, and gc.reflogExpire expires reflog entries older than 90 days.

SettingDefaultEffect on a dropped stash
gc.pruneExpire2 weeksUnreachable stash commit can be deleted after 14 days (2 weeks) when gc runs
gc.reflogExpire90 daysReflog entries older than 90 days expire
git stash clearimmediateAll entries become unreachable at once

Also, git fsck --no-reflogs --unreachable lists such commits if you lost the drop output (git fsck, git reflog).

9. What broke when I ran this

  • A forgotten entry. In my first pass I ran git stash push -m "wip header" and never popped it, so every later git stash list carried a stray wip header entry at the bottom and my numbering was off by one. I discarded that repository and re-ran everything from a fresh one so the outputs above are clean. Lesson: run git stash list before you start, not just after.
  • My pop did not restore the staged state. I expected git stash pop to bring back M and got M (section 3). --index fixes it.
  • A plain git stash left a new file in place (?? scratch.txt), so the tree was not clean and a branch switch would have carried it along. -u fixes it.
  • The conflict kept the entry. After CONFLICT (content) the stash was still there; if I had run git stash drop before resolving, the stashed version would have been gone.

Quick reference for when to pick which:

SituationUse
Switching branches for 10 minutesgit stash push -u -m "msg" then git stash pop
Not sure it applies cleanlygit stash apply, drop later
Stash holds staged workpop --index
Branch changed a lot sincegit stash branch name
Only one filegit stash push -m "msg" -- file

10. Conclusion

Stash is a short-term parking lot, not storage. My rule of thumb: a stash older than 1 day becomes a branch, and a stack deeper than 3 entries means I should have committed. Each entry is a full 40-character commit hash under the hood, and a dropped one survives only until the 2-week (14 days) prune window and the 90-day reflog window close. Anything that will live longer than a day belongs on a branch, a real commit I can amend or squash later. Always write a message, add -u when you created files, and run git stash list before and after.

This is lesson Day 14 of the DevOps Master Course on YouTube; the written companion for the whole course, including the interactive rebase and reset chapters, is the free Git and GitHub blueprint course. The official walkthrough is the Pro Git chapter on stashing and cleaning.

Did this save you a debugging session? Stay in the loop -