I'd like to be able to stash just the changes from a single file:
git stash save -- just_my_file.txt
The above doesn't work though. Any alternatives?
If you do not want to specify a message with your stashed changes, pass the filename after a double-dash.
$ git stash -- filename.ext
If it's an untracked/new file, you will have to stage it first.
However, if you do want to specify a message, use push
.
git stash push -m "describe changes to filename.ext" filename.ext
Both methods work in git versions 2.13+
git stash -- filename.ext
, git commit --amend
, git stash pop
Commented
Aug 12, 2019 at 22:30
$ git stash -m "message" -- filename.ext
works as well (git v2.31.1)
I think stash -p
is probably the choice you want, but just in case you run into other even more tricky things in the future, remember that:
Stash
is really just a very simple alternative to the only slightly more complex branch
sets. Stash is very useful for moving things around quickly, but you can accomplish more complex things with branches without that much more headache and work.
# git checkout -b tmpbranch
# git add the_file
# git commit -m "stashing the_file"
# git checkout main
go about and do what you want, and then later simply rebase
and/or merge
the tmpbranch. It really isn't that much extra work when you need to do more careful tracking than stash will allow.
git checkout tmpbranch the_file
from master makes this extra useful to pull single-file changes back into master branch. See stackoverflow.com/a/307872/4692594
Commented
Aug 17, 2020 at 22:57
$
down in the answer below). Both are routinely used, though sometimes $
implies a normal user and a #
implies a root/admin.
Commented
May 20, 2021 at 20:13
You can interactively stash single lines with git stash -p
(analogous to git add -p
).
It doesn't take a filename, but you could just skip other files with d until you reached the file you want stashed and the stash all changes in there with a.
git 2.14.1
you can specify file name git stash -p <filename>
git stash -p <filename>
does not work for me. Instead, git stash -- <filename>
worked.
The best option is to stage everything but this file, and tell stash to keep the index with git stash save --keep-index
, thus stashing your unstaged file:
$ git add .
$ git reset thefiletostash
$ git stash save --keep-index
As Dan points out, thefiletostash
is the only one to be reset by the stash, but it also stashes the other files, so it's not exactly what you want.
git stash pop
, isn't it going to apply all of the files, not just the one file that we wanted to stash?
Commented
Sep 14, 2012 at 12:36
Just in case you actually mean 'discard changes' whenever you use 'git stash' (and don't really use git stash to stash it temporarily), in that case you can use
git checkout -- <file>
Note that git stash is just a quicker and simple alternative to branching and doing stuff.
git stash
with discarding changes quickly. In fact, I googled the exact title of this post and didn't know I was incorrectly searching. It looks like multiple people have been in a similar boat and found my answer useful. Think of my answer as a prompt like "Did you mean X". After 8 years, I still stand by my opinion to have this prompt on this page even if I now know git-stash better.
Try this approach, which adds the file in question to the staging area and then simply stashes it:
git add ./path/file.ext
git stash --staged
After that you can do things like roll back all the other changes and bring back the stash afterwards:
git reset --hard
git stash pop
cp just_my_file.txt just_my_file.txt.manualstash
now you can do all the checkouts and stuff and as the copy is "untracked file", you can move across branches and commits without any problems. When you're on the right branch/commit where you want to "merge the single file" just domv just_my_file.txt.manualstash just_my_file.txt
and now you can review changes and commit it where necessarygit clean -f -d
in the meantime, as it removes untracked files. :-)