271

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?

Lii
  • 9,906
  • 6
  • 53
  • 73
EoghanM
  • 20,021
  • 21
  • 80
  • 110
  • 12
    possible duplicate of [How to stash only one file out of multiple files that have changed](http://stackoverflow.com/questions/3040833/how-to-stash-only-one-file-out-of-multiple-files-that-have-changed) – mlt Feb 04 '14 at 15:26
  • 1
    for _single_ file, instead of juggling stash commands, much easier approach is to copy _single_ file and when you want to bring it back to simply copy over the original. eg. `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 do `mv just_my_file.txt.manualstash just_my_file.txt` and now you can review changes and commit it where necessary – Dimitry K Oct 14 '16 at 15:43
  • @DimitryK Just don't do `git clean -f -d` in the meantime, as it removes untracked files. :-) – Danijel Oct 15 '20 at 10:58

5 Answers5

233

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 master

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.

Wes Hardaker
  • 19,386
  • 1
  • 35
  • 63
197

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+

sealocal
  • 6,810
  • 1
  • 31
  • 44
62

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.

Benjamin Bannier
  • 45,636
  • 10
  • 55
  • 78
  • 4
    This takes a long time if you have a lot of files you don't want to stash. – jjj Oct 04 '17 at 19:12
  • 31
    i don't know when this changed but as of `git 2.14.1` you can specify file name `git stash -p ` – muon Oct 18 '17 at 17:57
25

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.

CharlesB
  • 75,315
  • 26
  • 174
  • 199
  • 2
    Although this keeps the index in the same state, doesn't this also stash the files that are in the index? In other words, if after doing this you commit the index to the current branch, then switch to another branch and do `git stash pop`, isn't it going to apply all of the files, not just the one file that we wanted to stash? – Dan Moulding Sep 14 '12 at 12:36
  • @DanMoulding: you're absolutely right, I haven't thought about this. – CharlesB Sep 14 '12 at 12:40
14

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.

Devesh
  • 736
  • 7
  • 16
  • I think this is the best answer if you want to remove all your modified changes to a single file. – Mohamed Haseel Feb 11 '16 at 19:54
  • and i would this is the best answer for that particular task – Zakir hussain Nov 27 '18 at 13:52
  • 11
    The question was clear enough and didn't ask how to revert changes to one file. Downvoted. – Richard Smith Feb 12 '19 at 18:09
  • @MukulJain I actually needed to use the stash-per-file in a very-temporary fashion (basically separating some changes in two commits that affected the same lines, for one file). I won't actually downvote, just state that it's useless to me. – Paul Stelian Aug 22 '19 at 08:39