Git Stash: Saving Work Without Committing
Stash shelves uncommitted changes so you can switch context. Learn the commands, the pitfalls, and when a commit is better.
By Velloreth
The basic commands
Save changes:
git stash
This saves tracked, modified files and reverts the working tree to HEAD.
Save with a message:
git stash push -m "wip: checkout refactor"
List stashes:
git stash list
# stash@{0}: On main: wip: checkout refactor
# stash@{1}: WIP on main: a1b2c3d add validation
Apply the most recent and remove it:
git stash pop
Apply the most recent and keep it:
git stash apply
Apply a specific stash:
git stash apply stash@{1}
Show a stash’s diff:
git stash show -p stash@{0}
Drop a stash:
git stash drop stash@{0}
Clear all stashes:
git stash clear
What gets stashed
By default, git stash saves tracked files that have been modified or staged. It does not save:
- Untracked files — new files Git does not know about.
- Ignored files — files matched by
.gitignore.
Include untracked files:
git stash -u
Include untracked and ignored files:
git stash -a
The -u flag is the one you usually want. A new file that is not stashed stays in the working tree, which can cause a conflict when you switch branches.
The stack
Stashes are a stack. stash@{0} is the most recent.
stash@{0} ← most recent
stash@{1}
stash@{2} ← oldest
pop removes the top entry. apply leaves it. drop removes a specific entry.
Because it is a stack, applying an older stash while a newer one exists is easy to get wrong. Naming stashes with -m makes them identifiable.
Common workflows
Switch branches with uncommitted work:
git stash push -m "wip: half-done feature"
git checkout other-branch
# do work
git checkout -
git stash pop
Pull with local changes:
git stash
git pull
git stash pop
Test the committed state:
git stash
npm test
git stash pop
Move changes to a new branch:
git stash
git checkout -b new-branch
git stash pop
Conflicts on pop
A stash pop can conflict if the working tree has changed since the stash was created.
Auto-merging file.js
CONFLICT (content): Merge conflict in file.js
The stash is not dropped when a conflict occurs, which is deliberate — the changes are still in the stack. Resolve the conflict, then drop the stash manually.
# resolve the conflict
git add file.js
git stash drop
This is why apply is safer than pop: if something goes wrong, the stash is still there.
Stash versus a temporary commit
A stash is convenient, but it has real drawbacks.
It is easy to forget. A stash is invisible in the normal workflow. You can switch branches, work for a week, and never notice the stash is still there.
It is local. A stash is not pushed, so it does not survive a machine change and is not visible to teammates.
It is not a branch. You cannot easily see the changes in a diff against the current state, and you cannot push it.
A temporary commit is often better:
git commit -m "wip: half-done feature"
git checkout other-branch
# do work
git checkout -
git reset --soft HEAD~1
The commit is recorded, visible in the log, and cannot be forgotten. The reset --soft at the end restores the changes to the index.
For a quick context switch, a stash is fine. For anything longer than a few minutes, a commit on a branch is safer.
Recovering a dropped stash
Dropped stashes are not immediately garbage collected.
git fsck --no-reflogs | grep commit
This lists dangling commits, which include dropped stashes. Find the one you want and apply it.
git stash apply <commit-hash>
The stash commit has a distinctive message like “WIP on main: …”, which makes it identifiable in the list.
Stash internals
A stash is stored as a commit, which explains its behaviour.
When you run git stash, Git creates a commit with two or three parents: the current HEAD, the index, and optionally the untracked files. The commit is stored under refs/stash, and the reflog for that ref is the stash list.
This is why a stash can be applied like a patch, and why a dropped stash is recoverable with git fsck — it is a real commit object that is simply no longer referenced.
It also explains why a stash does not include ignored files by default: they are not part of the index or the working tree that Git tracks.
Stash and branches
A stash is not tied to a branch. You can create it on one branch and apply it on another, which is the basis of the “move my changes to a new branch” workflow.
git stash
git checkout -b new-branch
git stash pop
The changes are applied to the new branch, and the original branch is unchanged. This works because the stash is a commit, not a branch-specific state.
The caveat: if the two branches differ significantly, the pop may conflict. The stash records the changes relative to the commit it was created on, so applying it elsewhere can produce a conflict even when the change itself is simple.
When not to stash
There are cases where a stash is the wrong tool entirely.
A long-running change. If you will be away from the work for more than a few minutes, a commit on a branch is safer. A stash is invisible in the normal workflow, and it is easy to forget for days.
Work that needs review. A stash cannot be pushed, so it cannot be reviewed. If the change needs a second pair of eyes, commit it to a branch.
Work that needs to survive a machine change. A stash is local. If you switch machines, the stash does not come with you.
Work that is nearly done. If the change is close to complete, finish it and commit. A stash adds a step to the workflow for no benefit.
The pattern that works: stash for a genuine interruption of a few minutes, and commit to a branch for anything longer.
Practical guidance
- Use
-mto name stashes. - Use
-uto include untracked files. - Prefer
applyoverpopwhen unsure. - Check
git stash listperiodically. - Use a temporary commit for anything longer than a quick switch.
- Resolve conflicts, then drop the stash manually.
- Recover dropped stashes with
git fsck. - Do not rely on stash for long-term storage.
For the related guides, see git-undo-changes-guide and git-branching-strategies. For the broader context, git-merge-vs-rebase and git-commit-messages-guide.
Quotable summary
git stash saves uncommitted changes and reverts the working tree to HEAD, so you can switch branches or pull without conflicts. Stashes are a stack, so stash pop applies the most recent and removes it while stash apply keeps it, and apply is safer when you are unsure the apply will succeed cleanly. Untracked files are not stashed by default, so use -u to include them, otherwise a new file stays in the working tree and can conflict when you switch branches. A stash is local and easy to forget, so a temporary commit on a branch is often safer for anything longer than a quick context switch. Dropped stashes are often recoverable with git fsck, because they are not immediately garbage collected.