Git · note

How to git stash one file and keep the rest of the working tree

One file holds an experiment, the other two are ready to commit. A pathspec after git stash push parks exactly that file and leaves the rest of the working tree untouched.

Git line
DateDec 03, 2026
Read5 min
Tested withgit 2.43.0 on Ubuntu 24.04, scratch repository with three tracked files
Card catalog cabinet with one drawer pulled open (AI-generated image)
AI-generated image

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

shell
$ 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.c

The 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:

shell
$ 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
+b2

stash@{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.

shell
$ git stash push -q -- '*.h'
$ git stash show --name-only
config.h
include/types.h

The 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:

output
$ 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:

CommandWorking treeIndex
git checkout stash@{0} -- app.cupdatedupdated (file shows as staged)
git restore --source=stash@{0} -- app.cupdateduntouched

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:

output
$ 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.

Last stops · FAQ

Questions people ask

How do you stash only one file in Git?

Run git stash push -- path/to/file. Only that file is saved to the stash and reset to its HEAD version; every other modified file stays as it is. Add -m "message" so the entry is easy to find in git stash list. The pathspec form needs Git 2.13 or newer.

What is the difference between git stash pop and git stash apply?

Both write the stashed changes back into the working tree. pop then deletes the entry, apply keeps it, so apply is the safer choice when the same change should go onto two branches. If pop hits a conflict with local edits it refuses, prints that the entry is kept, and nothing is lost.

Can a single untracked file be stashed?

Yes, but only with -u (or --include-untracked): git stash push -u -- notes.txt. Without it, Git 2.43 answers with a pathspec error because a stash normally records tracked files only. After -u the file disappears from the working tree and comes back with git stash pop.

Next stop

Where this line continues