Stashing Work in Progress: push, pop, apply, Partial Stashes and Branches
Set unfinished work aside with git stash: push with messages, untracked files, staged-only and partial stashes, list and show, pop or apply with --index, conflicts on pop, taking one file from a stash, turning a stash into a branch, autostash, and exporting stashes in Git 2.51.
kvi5hnuCode Sprout team member8 min readOn this page
You're halfway through a change when something urgent comes up: a bug on main, a teammate's branch to review, a pull that refuses to run. You don't want to commit half-done work, and you don't want to lose it. git stash saves your uncommitted changes, both staged and unstaged, and returns your working tree to the last commit. Later, you apply them again, on the same branch or another.
Stash and pop
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add trails"
echo "Lake loop: 5 km, mostly flat" > lake.txt # unfinished work
git stash push -m "Lake description, half done"
git status --short; echo "(clean)"
echo "Ridge loop: 8.2 km" > ridge.txt; git commit -q -am "Fix ridge distance" # the urgent fix
git stash list
git stash pop
git log --oneline
Saved working directory and index state On main: Lake description, half done
(clean)
stash@{0}: On main: Lake description, half done
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: lake.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (20fcc972344c07cb5502f34e7e3f4868ae3a237a)
81d06c1 Fix ridge distance
60093b8 Add trails
git stash push, or plaingit stash, saves staged and unstaged changes to tracked files, then resets them. Add-m "message": without one, stashes are namedWIP on <branch>: <last commit>, which gets confusing fast.git stash popre-applies the most recent stash and deletes it from the list, if it applied without conflicts.git stash listshows all stashes, newest first, asstash@{0},stash@{1}and so on.
Stashes are a stack: each new one becomes stash@{0}, and older ones move down.
What gets stashed
By default, git stash leaves untracked and ignored files alone. They stay in your working tree, which is surprising when you expected a clean folder:
| Option | Also stashes |
|---|---|
| (none) | staged and unstaged changes to tracked files |
-u (--include-untracked) | untracked files too |
-a (--all) | untracked and ignored files, such as build outputs. Rarely what you want |
--staged (-S) | only staged changes, leaving unstaged ones in place |
--keep-index (-k) | everything, but also leaves staged changes in the working tree |
-p (--patch) | the hunks you choose, interactively |
-- <paths> | only changes to those files |
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add trails"
echo "Lake, flat" > lake.txt; git add lake.txt # staged
echo "Ridge, steep" > ridge.txt # unstaged
echo "Forest" > forest.txt # untracked
echo "=== before"; git status --short
git stash push -q --staged -m "staged lake edit"
echo "=== after --staged: only the staged change was taken"; git status --short
git stash push -q -u -m "ridge edit and forest page"
echo "=== after -u: untracked file taken too"; git status --short; echo "(clean)"
git stash list
echo "=== what's in each stash"
git stash show --include-untracked stash@{0}
git stash show stash@{1}
=== before
M lake.txt
M ridge.txt
?? forest.txt
=== after --staged: only the staged change was taken
M ridge.txt
?? forest.txt
=== after -u: untracked file taken too
(clean)
stash@{0}: On main: ridge edit and forest page
stash@{1}: On main: staged lake edit
=== what's in each stash
forest.txt | 1 +
ridge.txt | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
lake.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
git stash show lists the files a stash changes. Add -p to see the full diff, and --include-untracked (-u) to include untracked files it holds.
--keep-index is useful for testing exactly what you're about to commit: stash everything else, run the tests on the staged version, commit, then git stash pop.
pop versus apply, and --index
| Command | Effect |
|---|---|
git stash pop [stash@{n}] | apply, and remove the stash if it applied cleanly |
git stash apply [stash@{n}] | apply, and keep the stash, for applying to several branches or as a backup |
git stash drop [stash@{n}] | delete a stash |
git stash clear | delete all stashes |
By default, applying puts everything back as unstaged changes, even what was staged when you stashed. The exception is new files you had added: they come back as added, so they don't become untracked. --index restores the staged/unstaged split as well. Git 2.52 added the stash.index setting to make that the default.
Conflicts when applying
If the files changed since you stashed, applying can conflict, just like a merge. The conflict markers appear in the files, and pop keeps the stash in that case, so nothing is lost:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5 km, flat" > lake.txt
git stash push -q -m "Describe lake"
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Update distance"
git stash pop
echo "=== the stash is still there"
git stash list
echo "=== resolve, then drop it yourself"
echo "Lake loop: 5.4 km, flat" > lake.txt
git restore --staged lake.txt # back to an ordinary unstaged edit
git stash drop -q
git status --short; git stash list | wc -l
Auto-merging lake.txt
CONFLICT (content): Merge conflict in lake.txt
On branch main
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: lake.txt
no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.
=== the stash is still there
stash@{0}: On main: Describe lake
=== resolve, then drop it yourself
M lake.txt
0
After resolving, the changes are your working-tree edits. git restore --staged turns the "both modified" state back into a normal unstaged change, and you drop the stash yourself. To start over instead, git reset --merge discards the attempted application.
Taking part of a stash
To bring back one file from a stash, without applying the rest:
git restore --source='stash@{0}' -- ridge.txt # or: git checkout stash@{0} -- ridge.txt
git diff stash@{0}^1 stash@{0} -- ridge.txt # look at the stash's change to one file first
The quotes stop some shells from treating {0} specially. Note that restore --source takes the file's whole content from the stash, overwriting your current version, rather than merging the stashed change into it.
A stash as a branch
When a stash no longer applies cleanly, because the branch moved on, git stash branch <name> [stash] creates a branch from the commit you were on when you stashed, applies the stash there, and drops it on success:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5 km, flat" > lake.txt
git stash push -q -m "Describe lake"
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Update distance"
git stash branch lake-description > /dev/null
echo "=== the new branch starts at the stash's commit, with the change applied"
git branch -v
cat lake.txt
Switched to a new branch 'lake-description'
=== the new branch starts at the stash's commit, with the change applied
* lake-description 65a5265 Add lake page
main c4f0314 Update distance
Lake loop: 5 km, flat
The changes apply without conflicts, because the branch starts at the commit they were made on. Commit them there, then merge or rebase the branch as usual. This is also the answer when a stash should really have been a branch all along.
Stash or commit?
Stashes are meant to be short-term. They aren't pushed, they're easy to forget, and they have no context beyond a message. For anything you'll come back to after more than a short while:
- Commit it on a branch, even as a "WIP" commit, and amend or squash it later (article 15). Unlike a stash, a commit can be pushed as a backup.
- Use a worktree to work on the urgent task in a separate folder, leaving your current work untouched (article 40).
Autostash
Several commands can stash and restore for you:
git pull --autostash # or: git config --global rebase.autoStash true
git rebase --autostash main
git merge --autostash topic
git switch -m other-branch # carry local changes, merging them into the other branch
--autostash creates a temporary stash, runs the operation, and applies the stash afterwards. If applying conflicts, the stash is kept in the list, as with pop. rebase.autoStash and merge.autoStash turn it on for those commands, and since Git 2.51 pull.autostash sets it for git pull specifically.
How stashes are stored
A stash is a commit, in fact two or three: one for the working tree state, whose parents are the commit you were on and a commit holding the index state, plus a third parent for untracked files with -u. They're stored in the reflog of the reference refs/stash. That's why stash@{1} looks like reflog syntax, why git log stash@{0} works, and why dropped stashes can be recovered with git fsck (article 26).
Since Git 2.51, git stash export and git stash import turn the whole stash list into an ordinary chain of commits under a reference, and back. That makes stashes something you can push, fetch and move between machines:
git stash export --to-ref refs/stashes/laptop
git push origin refs/stashes/laptop
# on the other machine:
git fetch origin refs/stashes/laptop:refs/stashes/laptop
git stash import refs/stashes/laptop
What's current
git stash push(Git 2.13) replacedgit stash save, which is deprecated. It accepts paths, and-m.--staged(Git 2.35) stashes only staged changes.git stash export/import(Git 2.51) move stashes between repositories.stash.index(Git 2.52) makesapplyandpoprestore staged changes as staged.pull.autostash(Git 2.51) sets autostash forgit pullonly.
Common questions
Why didn't git stash take my new file?
New files are untracked, and stash ignores untracked files by default. Use git stash -u. Or git add the file first, so it's tracked.
I popped a stash and got conflicts. Is my stash lost?
No. When pop conflicts, it keeps the stash. Resolve the conflicts, then git stash drop it. Or run git reset --merge to undo the attempt, leaving the stash in place.
How do I see what's in a stash before applying it?
git stash show -p stash@{1} for the full diff, and add --include-untracked for untracked files.
Can I apply a stash on a different branch?
Yes. Stashes aren't tied to a branch: switch, then git stash apply or pop. Conflicts are possible if the files differ.
Can I push a stash?
Not directly. Make it a branch with git stash branch, commit and push, or, with Git 2.51 or later, use git stash export --to-ref and push that reference.
Summary
git stash push -m "msg"saves uncommitted changes and cleans the working tree.popbrings them back and drops the stash, andapplykeeps it.-uincludes untracked files,--stagedstashes only staged changes,--keep-indexleaves the staged changes in place, and-por paths stash part of the work.--index(orstash.index=true) restores what was staged as staged.- A conflicting
popkeeps the stash. Resolve, thendrop. git stash branch <name>applies a stash on a branch created where it was made.git restore --source=stash@{n} -- <file>takes one file from a stash.- For longer-lived work, prefer a WIP commit on a branch, or a worktree.
Practice questions
You stashed, but your new file forest.txt is still in the working tree. Why, and how do you stash it too?
Answer
forest.txt is untracked, and git stash only stashes changes to tracked files by default. Run git stash push -u (or --include-untracked). If you already stashed the rest, that creates a second stash containing just the untracked file.
You want to commit only the staged part of your work, but first run the tests on exactly that. How can stash help?
Answer
git stash push --keep-index -u stashes everything, including untracked files, but leaves the staged changes in the working tree. The folder now contains exactly what would be committed. Run the tests, git commit, then git stash pop to bring back the rest.
git stash pop fails with conflicts because main changed a lot since you stashed. What's a cleaner alternative?
Answer
First undo the attempt with git reset --merge. The stash is still in the list. Then run git stash branch <name>, which creates a branch at the commit the stash was made on and applies it there without conflicts. Commit, then merge or rebase that branch onto main, resolving the conflicts as part of a normal merge or rebase.
Previous: Recovering Lost Work With the Reflog: Commits, Branches, Rebases and Stashes

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.