Git Stash Explained: Save, Apply, Pop and Untracked Files, Tested
Git stash puts your unfinished changes on a shelf so you can switch tasks with a clean working tree, then bring them back later. Learn every everyday stash command, what each one really keeps, and how to recover from conflicts, with output we ran on Git 2.56.
- Git stash saves tracked changes and staged changes, then resets your working tree to the last commit.
- Untracked files are left behind unless you add
-u; in our test,todo.txtstayed in place after a plain stash. applykeeps the stash entry andpopdeletes it, except after a conflict, when Git kept the entry for safety.git stash branchturns a stash into a new branch and is the cleanest escape from a conflicting pop.
You are halfway through a feature when an urgent bug report lands. Your changes are not ready to commit, but you need a clean branch now. Git stash is built for this moment: it records your work in progress, cleans your working directory, and lets you restore everything later, on the same branch or a different one.
We ran every command below on Git 2.56 in a throwaway repository, using a demo identity so no personal details appear. The outputs are copied from that run. If you are still learning the basics of commits and branches, start with our Git tutorial for beginners.
What is git stash?
git stash takes the changes in your working tree and index (the staging area), saves them as special commits on a stack, and resets your files to match HEAD. Stashes are local: they are never pushed to a remote. Each entry gets a name such as stash@{0}, where 0 is always the newest.
Under the hood, a stash entry is a small merge commit: one parent is your HEAD, another records the index, and an optional third records untracked files. That is why stashes survive restarts and can be inspected like any other commit.
Our test repository
We committed one file, then created three kinds of change: a modified tracked file, a new staged file and a new untracked file:
$ git status --short
M app.txt
A staged.txt
?? todo.txt
The left column shows the index and the right shows the working tree. M is modified, A is added to the index and ?? is untracked.
Stashing changes with a message
The modern form is git stash push. Always add a message with -m; “WIP on main” tells you nothing a week later.
$ git stash push -m "wip: price field"
Saved working directory and index state On main: wip: price field
$ git status --short
?? todo.txt
Both the modified app.txt and the staged staged.txt disappeared into the stash. The untracked todo.txt did not. Plain git stash with no arguments does the same as push without a message.
Stashing untracked files
New files that Git has never tracked are ignored by default. Add -u (--include-untracked) to take them too:
$ git stash push -u -m "wip: todo"
Saved working directory and index state On main: wip: todo
$ git status --short
(nothing: the working tree is clean)
Use -a (--all) only if you also want ignored files, such as build output, which is rarely what you mean. To stash only some files, list them after --, for example git stash push -m "css" -- styles.css, or use -p to choose hunks interactively.
Tip: forgetting -u is the most common stash surprise. The new file stays behind, follows you to the other branch, and may even be committed there by accident.
Listing and inspecting stashes
We now had two entries. The newest is always stash@{0}, so older entries shift up by one every time you stash again:
$ git stash list
stash@{0}: On main: wip: todo
stash@{1}: On main: wip: price field
To see what a stash contains before restoring it, use show. Add -p for the full diff:
$ git stash show -p stash@{1}
diff --git a/app.txt b/app.txt
--- a/app.txt
+++ b/app.txt
@@ -1 +1,2 @@
title = "Shop"
+price = 10
diff --git a/staged.txt b/staged.txt
new file mode 100644
+staged line
By default show ignores the untracked part. For the second stash, git stash show --include-untracked --stat stash@{0} listed todo.txt | 1 +, which confirms the file really was saved.
git stash apply vs git stash pop
Both commands restore a stash. The difference is what happens to the entry afterwards:
$ git stash apply stash@{1}
$ git stash list
stash@{0}: On main: wip: todo
stash@{1}: On main: wip: price field (still there)
$ git stash pop --index stash@{1}
Dropped stash@{1} (7899bba...)
$ git stash list
stash@{0}: On main: wip: todo
| Command | Restores changes | Removes the entry | Best for |
|---|---|---|---|
git stash apply |
Yes | No | Applying the same changes to several branches, or playing safe |
git stash pop |
Yes | Yes, if no conflict | Everyday “put it back” use |
Without a name, both use stash@{0}. When you have several stashes, name the one you want; popping the wrong entry onto the wrong branch is easy.
Keeping staged changes staged
By default a restore puts modified files back as unstaged changes, so your careful staging is lost. Add --index to restore the staging area as it was. In our run, the new file came back staged either way. For a modified file that was partly staged (status MM), a plain pop came back as M, with the staging lost, while pop --index restored MM exactly. A related option, git stash push --keep-index, stashes everything but leaves the staged changes in place, which is handy for testing exactly what you are about to commit.
When pop hits a conflict
If the branch changed the same lines since you stashed, restoring causes a conflict. We stashed a title change, then committed a different title, then popped:
$ git stash pop
Auto-merging app.txt
CONFLICT (content): Merge conflict in app.txt
The stash entry is kept in case you need it again.
$ git status --short
UU app.txt
Git refused to drop the entry because the restore did not finish cleanly. Resolve the conflict markers as you would after a merge, stage the file, and then run git stash drop yourself once you are happy. If you would rather abandon the attempt, git checkout -- . or git reset --hard returns to the commit; the stash is still safe. Our merge vs rebase guide covers conflict resolution in more depth.
Turning a stash into a branch
When a stash no longer applies cleanly, create a branch from the commit where you made it:
$ git stash branch rename-fix stash@{0}
Switched to a new branch 'rename-fix'
Changes not staged for commit:
modified: app.txt
Dropped stash@{0} (900b8b7...)
Because the new branch starts at the original base commit, the changes apply without conflict. Commit them there and merge or rebase later. This is also the best move when a “quick stash” turns into real work that deserves its own branch.
Dropping and clearing stashes
$ git stash drop stash@{0}
Dropped stash@{0} (17ef58b...)
$ git stash clear
$ git stash list
(empty)
drop removes one entry and clear removes them all, with no confirmation. If you drop something by mistake, the hash printed by drop can still restore it for a while with git stash apply <hash>, until Git’s garbage collection removes unreachable objects. Copy that hash before closing the terminal. For other undo tools, see our git reset vs revert guide.
Good stash habits
- Keep the stack short. More than two or three entries usually means work that belongs on a branch.
- Always use messages. They are the only clue to what an entry holds.
- Check before you pop. Run
git stash show -pand confirm the current branch. - Prefer a commit for long breaks. A work-in-progress commit on a feature branch is backed up when you push; a stash is not.
- Consider worktrees.
git worktree addgives you a second checkout for urgent fixes with no stashing at all.
Git stash cheat sheet
| Goal | Command |
|---|---|
| Stash with a message | git stash push -m "msg" |
| Include untracked files | git stash push -u |
| Stash specific files | git stash push -- path |
| List stashes | git stash list |
| Show a stash’s diff | git stash show -p stash@{n} |
| Restore and keep | git stash apply stash@{n} |
| Restore and remove | git stash pop stash@{n} |
| Restore staging too | git stash pop --index |
| Stash into a new branch | git stash branch name stash@{n} |
| Delete one or all | git stash drop, git stash clear |
Final thoughts
Git stash is the fastest way to set work aside, as long as you remember what it does and does not keep. Name every stash, add -u for new files, inspect before you restore, prefer apply when unsure, and reach for git stash branch when a restore fights back. For the rest of your daily commands, keep our Git commands cheat sheet close.
Frequently asked questions
What does git stash do?
It saves your uncommitted tracked and staged changes on a local stack and resets the working tree to the last commit, so you can switch tasks and restore the changes later.
What is the difference between git stash pop and apply?
Both restore the changes. pop also deletes the stash entry when it applies cleanly, while apply keeps the entry so it can be reused or kept as a backup.
Does git stash include untracked files?
No, not by default. Use git stash push -u or –include-untracked to stash new untracked files, and -a to include ignored files too.
How do I recover a dropped stash?
Use the commit hash that git stash drop printed with git stash apply followed by the hash. It works until garbage collection removes the unreachable commit.
Are stashes pushed to the remote repository?
No. Stashes exist only in your local repository. Commit work to a branch and push it if you need a backup or want to share it.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.