The situation in the test repository: app.c and README are finished, config.h carries a debugging tweak that must not land in the next commit. Plain git stash would take all three files. Since Git 2.13, git stash push accepts a pathspec, and a pathspec is all you need to git stash one file.
Stash one file with git stash push
$ git status --short
M README
M app.c
M config.h
$ git stash push -m "wip config" -- config.h
Saved working directory and index state On main: wip config
$ git status --short
M README
M app.cThe manual describes it precisely: the new entry records only the files that match the pathspec, and only those files are rolled back to HEAD. README and app.c are not touched.
The -- separates options from paths. It is not strictly required for a normal file name, but it removes any doubt when a file happens to start with a dash. The -m message is worth the extra keystrokes. Without one, the entry is labeled WIP on main: plus the hash and subject of the current commit, which says nothing about what is inside.
Checking the result:
$ git stash list
stash@{0}: On main: wip config
$ git stash show -p stash@{0}
diff --git a/config.h b/config.h
index 6178079..b89df23 100644
--- a/config.h
+++ b/config.h
@@ -1 +1,2 @@
b
+b2stash@{0} is always the newest entry. Every further push shifts the older ones up by one, so stash@{0} today is stash@{1} after the next stash, and the message is the only stable way to recognize an entry.
The word push can be left out, but then the -- is mandatory: git stash -- app.c behaved exactly like the long form. Without --, Git refuses extra arguments after a bare git stash so that a typo in a subcommand name does not quietly create an entry. The older git stash save is deprecated and never took paths at all: in the test, git stash save -- app.c stashed every modified file and used -- app.c as the message.
Several files, a folder or a pattern
Anything Git calls a pathspec works: a list of files, a directory, or a quoted glob.
$ git stash push -q -- '*.h'
$ git stash show --name-only
config.h
include/types.hThe quotes keep the shell from expanding *.h itself. Git's own matching allows * to cross directory separators, which is why include/types.h was caught along with the top-level header. Without quotes, the shell would have expanded the pattern to config.h only.
Getting the file back
git stash pop writes the change back and deletes the entry. git stash apply writes it back and keeps the entry. If the file was edited again in the meantime, pop does not merge blindly:
$ git stash pop
error: Your local changes to the following files would be overwritten by merge:
app.c
Please commit your changes or stash them before you merge.
Aborting
...
The stash entry is kept in case you need it again.Commit or discard the new edit, then pop again. git stash show -p beforehand shows exactly which lines are about to come back, which helps decide whether the newer edit or the stashed one should win.
One file out of a stash that holds many
When a stash contains five files and only one of them is needed, there is no reason to apply the whole entry. Two commands copy a single path out of it, and they differ in a detail:
| Command | Working tree | Index |
|---|---|---|
git checkout stash@{0} -- app.c | updated | updated (file shows as staged) |
git restore --source=stash@{0} -- app.c | updated | untouched |
git restore arrived in Git 2.23. In both cases the entry stays in the stash list until git stash drop removes it.
Untracked files need -u
A brand new file is invisible to a normal stash, and the error message does not say so clearly:
$ git stash push -- notes.txt
error: pathspec ':(,prefix:0)notes.txt' did not match any file(s) known to git
Did you forget to 'git add'?The alternative: stage it, then stash --staged
Git 2.35 added --staged, which stashes whatever is in the index and nothing else. For one file the flow is git add config.h followed by git stash push --staged -m "wip config". In the test repository the result was identical to the pathspec version. It pays off when the set of files is easier to build with git add -p than to describe as a path list. There is one oddity, though: popping that entry put config.h back as a modified, unstaged file, not a staged one.
A partially staged file loses its split
That oddity has a sharper version. Take a file with one line staged and a second line still unstaged, so git status --short shows MM config.h. After git stash push -- config.h the file is clean. After a plain git stash pop it is back with both lines, but as M: everything unstaged, the careful split gone. Only git stash pop --index restored the MM state, with the first line in the index and the second in the working tree. And the manual warns that even --index can fail once the pop runs into a conflict: conflicts are stored in the index, so the old staged state can no longer be put back as it was.
