Git Stash Single File

How to manage individual files in the Git Stash

Seann Hicks

Seann Hicks

Sunday, June 27, 2021

Git Stash Single File

To stash a specific file in git use the following command:

git stash push -- myfile.txt

This command can be short formed as:

git stash -- myfile.txt

Stashing changes to a single file is the same as making a change to only that file and stashing all changes. Note that you need to have an initial commit and thus a HEAD revision in order to stash files.  See my article, What is Git Head, for more details on Git Head.

If you have a freshly initiated folder with no initial commit you will not be able to stash until you create your first commit (do a Git Add, then a Git Commit). The dashes indicate that the next argument is a file path specification (pathspec) and when using the push command, are only needed if you have a file name that could be confused with a branch name or git command.

Git stashing single files is useful when you want to pick and choose which files to stash from you working directory changes.  

The Git Stash is a holding area for in progress changes that you want to preserve but need keep out of the way temporarily.  For example, you may want to change branches.  This is commonly where I use the git stash.  it works like a stack where changes are pushed and popped off the stash.

git stash pop

You can also restore the stashed changes into your current branch with the git stash apply command.

git stash apply

Other Git Stash Commands

There are a number of ways to control the Git stash.  Git stash save is the deprecated version of Git stash push.

git stash push -- file1.txt
git stash save -- file1.txt

Files can be stashed with comments.

git stash push -m "Change comment" -- file1.txt

To see the contents of the git stash use the git stash list command:

git stash list

It is possible to create merge issues with the stash. For example, if you change a file, stash it, then change the same file again and stash it. The pop or apply command expects to overwrite the existing file but if it has changes you run into a conflict.

when you run the git stash list command you'll see an output like this:

stash@{0}: WIP on master: e85282c Initial Revision
stash@{1}: WIP on master: e85282c Initial Revision
stash@{2}: WIP on master: e85282c Initial Revision
stash@{3}: WIP on master: e85282c Initial Revision

Here we can see my initial revision on the master branch and the hash e85282c. The stash list shows an index number between the curly braces.

It is possible to apply a specific revision in the stash with the apply command.

git stash apply "stash@{2}"

This also works for git stash pop.


You can see that the Git stash offers fine grained manipulation and the ability to track specific changes on single files if needed through the git stash push command.

Photo by Juliana Kozoski on Unsplash

Sign-up to receive the weekly post in your inbox. I don't share email addresses, unsubscribe at any time.

Email Address