Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stash just a single file

Tags:

git

git-stash

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?

like image 661
EoghanM Avatar asked Sep 14 '12 08:09

EoghanM


2 Answers

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+

like image 153
sealocal Avatar answered Sep 19 '22 05:09

sealocal


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.

like image 35
Wes Hardaker Avatar answered Sep 21 '22 05:09

Wes Hardaker