Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Git stash saves your uncommitted work on a stack and returns the working tree to the last commit, so you can switch branches and come back. Per the Pro Git book, stashing “takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).” The command to save is git stash push; the commands to bring work back are git stash pop and git stash apply. The part most guides skip, covered below, is what a stash does not save and what happens when applying it hits a conflict.
What stashing is used for in research
Analysis code changes constantly while a paper, thesis or grant deliverable is in progress. Stashing fits three recurring situations:
- Switching branches mid-edit. You are halfway through changing a model script and need to rerun the version on another branch. Stash, switch, rerun, switch back, pop.
- Setting aside one file. A pathspec limits the stash to the files you name, so a half-finished data-cleaning script can be parked while the rest of the tree stays as it is.
- Pausing exploratory edits. Work you are not ready to commit stays recoverable instead of being overwritten. For the opposite goal, throwing changes away, see how to discard local changes in Git.
Stashing is a local convenience, not a record of your work. Results that a publication depends on belong in commits; see computational reproducibility for why a recoverable, labelled history matters.
Install or access
git stash is part of Git itself, so there is nothing extra to install. Git is distributed from the official site, git-scm.com, which also hosts the command reference (git-stash) and the free Pro Git book. Run the commands below inside a Git repository.
Step-by-step: stash and restore your work
The synopsis in the official manual is git stash [push] [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q] [-u|--include-untracked] [-a|--all] [(-m|--message) <message>] [--pathspec-from-file=<file>] [--] [<pathspec>...].
- Save your changes with a label. Run
git stash push -m "describe the work". Per the manual,pushsaves “your local modifications to a new stash entry and roll[s] them back to HEAD”;-madds an optional description. - Include untracked files if you need them. By default only modified and staged tracked files are stashed (Pro Git). Add
-u(--include-untracked) to also stash untracked files; they are “cleaned up with git clean”. Add-a(--all) to include ignored files as well. - Stash only some files. Name them after the options:
git stash push -m "wip cleaning" -- scripts/clean.R. The manual states the new entry “records the modified states only for the files that match the pathspec.” - Stash only part of a file or only staged changes.
-p(--patch) lets you choose hunks interactively;-S(--staged) stashes only what is currently staged. If both are given,--patchtakes priority. - List your stashes. Run
git stash list. - Look inside one. Run
git stash showfor a diffstat of the latest entry, or name one:git stash show stash@{1}. Add-uto include untracked files in the display (configstash.showIncludeUntrackeddefaults to false). - Bring the work back. Either
git stash pop(apply it, then remove it from the list) orgit stash apply(apply it and keep it on the stack). Pro Git: “The apply option only tries to apply the stashed work — you continue to have it on your stack.” - Restore staged state too. Add
--indextopoporapplyto reinstate the index changes as well as the working tree; the manual notes this can fail on conflicts. The configstash.indexdefaults to false. - Remove entries.
git stash dropremoves one;git stash clearremoves all. The manual warns that cleared entries “may be impossible to recover”, so only clear once you are sure.
The old form git stash save is deprecated “in favour of git stash push” in the manual, and save cannot take a pathspec.
Output and how to read it
Each entry in git stash list looks like the manual’s example:
stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{0}is the entry name.stash@{0}is the newest,stash@{1}the one before it. A bare integernmeansstash@{n}, and no argument meansstash@{0}.- Time syntax also works, for example
stash@{2.hours.ago}. WIP on submitis the branch the stash was made on, followed by the commit it started from. A label from-mreplaces the generic text, which is why labelling helps once you have several entries.- After
git stash dropor a successfulpop, Git prints a line in the formDropped stash@{0} (364e91f3...)as shown in Pro Git.
Troubleshooting
Why did pop fail with a conflict, and is my stash gone?
No. The manual states that applying a stash can fail with conflicts, and in that case “it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.” apply, pop and branch exit with status 1 on conflicts and leave the entry in place.
Why are my new data files missing from the stash?
Untracked files are not stashed unless you pass -u, and ignored files (large outputs, for instance) are not stashed unless you pass -a. Check with git stash show -u before you rely on an entry.
How do I stash a single file?
Use a pathspec after --, as in step 3 above.
How do I get the stash onto its own branch?
git stash branch <branchname> [<stash>] creates and checks out a new branch starting from the commit where the stash was created, reapplies the work, and drops the stash if that succeeds. It is the route when the branch you stashed on has moved on and pop conflicts.
Did I apply the wrong stash?
Run git stash list and name the entry explicitly, for example git stash apply stash@{2}. Use apply rather than pop when you are unsure, because it keeps the entry.
Free alternatives and when to use each
Git is free, and so is every option below; they differ in how much they keep and where.
| Option | What it does | Source |
|---|---|---|
git stash push then pop |
Saves tracked changes on the stack and removes them from the tree; pop applies and removes the entry. | git-scm.com git-stash; Pro Git |
git stash apply |
Applies the entry and keeps it on the stack. | git-scm.com git-stash; Pro Git |
git stash branch |
Turns a stash into a new branch from its original commit, dropping the stash on success. | git-scm.com git-stash; Pro Git |
git stash push -- <pathspec> |
Records only the files that match the pathspec. | git-scm.com git-stash |
| Discard instead of save | Throws the changes away rather than keeping them; see discarding local changes in Git. | CASRAI guide |
For versioning large datasets alongside code, stashing is not the tool; see DVC data version control for research datasets. A related habit for sharing a working setup is covered in sharing a reproducible conda environment.
FAQ
What is the difference between git stash pop and git stash apply?
pop applies the stashed state and removes it from the stash list. apply applies it and leaves it on the list. If either hits conflicts, the entry stays in place.
How do I stash untracked files?
Add -u (--include-untracked) to git stash push; add -a (--all) to include ignored files too.
How do I stash a single file?
Run git stash push -- path/to/file. Only files matching the pathspec are recorded in the entry.
How do I see what is in a stash?
Run git stash show (latest) or git stash show stash@{n}. The default is a diffstat; -u includes untracked files.
Can I stash on one branch and apply on another?
Yes. Pro Git says a stash can be reapplied “even on a different branch”.
Sources
- Git project, git-stash manual page, git-scm.com, checked 2026-10-29.
- Scott Chacon and Ben Straub, Pro Git, “Stashing and Cleaning”, git-scm.com, checked 2026-10-29.







