Save only one file

I would like to be able to save only changes from a single file:

git stash save -- just_my_file.txt 

The above does not work. Any alternatives?

+103
git git-stash
Sep 14
source share
5 answers

I think stash -p is probably the choice you want, but just in case you come across other more complex things in the future, remember that:

Stash really just a very simple alternative to just slightly more complex branch sets. Stash is very useful for quickly moving things, but you can do more complex things with branches without this much bigger headache and work.

 # git checkout -b tmpbranch # git add the_file # git commit -m "stashing the_file" # git checkout master 

go in and do what you want, and then just rebase and / or merge tmpbranch. This is actually not a lot of extra work when you need to do more thorough tracking than stash allows.

+120
Sep 14 '12 at 13:15
source share

You can interactively hide individual lines using git stash -p (similar to git add -p ).

No file name is required, but you can simply skip other files with d until you reach the file you want to hide and all changes there will be a .

+48
Sep 14
source share

The best option is to create everything except this file, and tell stash to save the index with git stash save --keep-index , thereby knocking down your uninstalled file:

 $ git add . $ git reset thefiletostash $ git stash save --keep-index 

As Dan points out, thefiletostash is the only one that needs to be reset to stash, but it also gets stuck in other files, so it's not exactly what you want.

+17
Sep 14 '12 at 8:45
source share

Just in case, you really mean “discard changes” whenever you use “git stash” (and don't really use git stash for temporary storage), in which case you can use

 git checkout -- <file> 

Note that git stash is just a simpler and simpler alternative to branching and working.

+10
Dec 19 '14 at 12:14
source share

If you do not want to indicate a message with your hidden changes, pass the file name after the double dash.

 $ git stash -- filename.ext 

If it is an untracked / new file, you will have to post it first.

However, if you want to specify a message, use push .

 git stash push -m "describe changes to filename.ext" filename.ext 

Both methods work in git version 2. 13+

+7
Mar 09 '19 at 3:59
source share



All Articles