Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Git Stash: push, pop, apply, and Partial Stashing

A stash is a commit with two or three parents, and it leaves untracked files behind by default. When a temporary branch or a worktree is the better tool.

~5 min readGit
#Git#Stash#Workflow#CLI
3D illustration of a drawer holding modified file cards beside a stack of numbered stashes

Quick Summary

A stash is a commit — two or three of them, in fact, stored under refs/stash with the working tree and index captured as separate parents. git stash push cleans the working tree and records it; pop applies it and drops it; apply applies it and keeps it in the list.

The default stash covers tracked files only. Untracked files need -u, ignored files need -a, and forgetting that is how new files get "lost" in a conflict — they were never in the stash.

Stashes are a stack, addressed by index (stash@{0} is newest), and they apply to whatever branch is checked out, not the branch they were created on. For work that will live longer than an hour, a temporary branch is safer: a stash has no reflog of its own beyond the shared one, and a mistaken drop or clear needs git fsck to recover.

A stash is also not a backup. It lives in the same repository as the work, in objects that ordinary git gc may prune once the entry is dropped, and it is never pushed by git push. Anything that must survive a laptop failing belongs in a commit on a remote branch, even if the branch is named wip/scratch and deleted next week.

Basic Syntax and Core Signatures

bash
git stash push # tracked changes git stash push -m "wip: login form" # with a message, always do this git stash push -u # include untracked files git stash push -a # include ignored files too (rarely) git stash push -k # keep the index staged, stash the rest git stash push --staged # stash only what is staged (2.35+) git stash push -p # hunk by hunk git stash push -- src/parser.ts # only these paths git stash list # stash@{0}: On main: wip: login form git stash show # diffstat of the newest git stash show -p stash@{1} # full patch git stash apply # apply, keep in the list git stash apply --index # restore staged/unstaged split git stash pop # apply and drop on success git stash pop stash@{2} git stash drop stash@{1} git stash clear # drop everything, no prompt git stash branch fix-login stash@{0} # new branch from the stash's original base

git stash branch is the underused one: it creates a branch at the commit the stash was made on, applies the stash there, and drops it if the apply is clean. That is the correct response when a stash no longer applies to the current branch.

Key Comparison Table

CommandWorking treeStash listConflicts possibleUse when
pushCleanedOne more entryNoPark work to switch branches
popChangedEntry removed on successYesYou want the work back now
applyChangedEntry keptYesYou may want it on two branches
branch <name>Changed on a new branchRemoved on clean applyYesThe stash no longer applies
dropUntouchedOne fewer entryNoThe work is no longer needed
clearUntouchedEmptyNoCleanup, irreversible
FlagEffectTrap
-u / --include-untrackedAdds new filesWithout it, new files are not stashed
-a / --allAdds ignored files too.env and build output land in the stash
-k / --keep-indexLeaves staged changes in the treeTest runs see only staged code
--stagedStashes only the indexWorking tree changes stay
-- <pathspec>Scopes to pathsPaths must come after --

Practical Edge Cases and Deep Dives

1. The stash that does not include your new files

git stash push then git switch main succeeds, and the two files you just created stay in the working tree because they were untracked — they then follow you to main as untracked files, and if main has tracked files at those paths the switch fails. Use -u and check git status --porcelain after the stash to confirm a clean tree.

2. pop with a conflict keeps the stash

A conflicted pop leaves the stash in the list and the working tree with conflict markers. That is deliberate: the entry is your only copy. Resolve, git add, then git stash drop explicitly. Scripts that assume pop always removes the entry end up with duplicates.

bash
git stash pop # conflict # resolve files git add -A git stash drop # now it is safe

3. Applying a stash to a different branch

Stashes are not branch-scoped. git stash apply on another branch replays the diff as it was, so a change that depended on a renamed symbol produces either a conflict or, worse, a clean apply that compiles but is semantically wrong. git stash show -p before applying is cheap insurance.

Preserving the staged split. A plain apply merges everything into the working tree, losing which hunks were staged. --index restores the index too, but fails if the index has diverged. For a partial-stash workflow — stage a subset, stash the rest, commit on another branch — --index is what makes the round trip lossless.

4. Partial stashing by path and by hunk

git stash push -p walks hunks like add -p, and git stash push -- path/ scopes by path; both leave the rest of the tree untouched. Combined with -k, this is the standard way to park an unrelated refactor while committing a hotfix:

bash
git stash push -k -m "refactor: extract parser" -- src/parser.ts git commit -m "hotfix: null check" git stash pop

5. Stash as a commit, and recovering a dropped one

refs/stash holds a commit whose first parent is HEAD at stash time and whose second parent is the index commit; a third parent exists when untracked files are included. That means git show stash@{0}:path/file reads a file from the stash, and a dropped stash is recoverable with git fsck --unreachable | grep commit followed by git stash apply <sha> — as long as garbage collection has not run.

6. Choosing between stash, branch, and worktree

The three tools overlap, and picking by habit is why people run out of stash entries. A stash is right for minutes-to-hours: a hotfix interrupts a refactor, you need a clean tree for a test run. A wip/ branch is right for days: the work needs review, a colleague needs it, or the machine may be replaced. A worktree is right when both must exist at once — an old release to patch while main keeps moving:

bash
git worktree add ../app-release-2.4 release/2.4 git worktree list git worktree remove ../app-release-2.4

A worktree has its own index and working tree but shares the object database and refs, so the two checkouts cannot hold the same branch at the same time. That is a feature: the branch checkout is unambiguous, and switching no longer requires stashing at all.

refs/stash is also visible to tooling: a stash entry appears in git fsck output, survives git gc as long as its reflog entry exists, and is copied by git clone --mirror but not by a normal clone. That asymmetry explains why a dropped stash is recoverable on the machine where it was created and nowhere else.

Hygiene. A stash is local state that no review process sees, so it accumulates the same way dotfiles do. Two simple rules keep it from becoming a mystery: every stash carries a message (-m) naming the branch and the intent, and nothing survives a week. A short cleanup pass is enough:

bash
git stash list --date=iso | tail -20 git stash show -p stash@{5} | head -40 # decide: branch, reapply, or drop git stash drop stash@{5}

In CI, a stash is almost always a mistake. Build agents get a fresh checkout per job, so a stash there is invisible work that disappears when the container ends. If a pipeline needs to park changes between steps, commit to a throwaway branch with an explicit name — then the step's state is visible in the log, and the next failure is diagnosable.

Common Git Operation Mistakes

SymptomCauseFix
New files missing after a stashpush without -uRe-add them; use -u next time
.env captured in a stash-a usedgit stash show -p to inspect, drop and redo
Stash applies with conflicts every timeBase commit is far behindgit stash branch <name> <stash>
Two copies of the same changeapply used instead of popgit stash drop after verifying
Staged/unstaged split lostapply without --indexUse --index next time, re-stage manually
Stash list grown to dozens of entriesNever cleaned upgit stash list, review, drop or convert to branches
Stash lost after clearNo prompt on cleargit fsck --unreachable, apply the dangling commit
Stashed work not in the branch you expectedStashes are not branch-scopedCheck git stash list messages, apply to the right branch

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.