Git: Stash just a single file

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?


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.


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.


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.

链接地址: http://www.djcxy.com/p/24504.html

上一篇: 你如何告诉git只存储索引?

下一篇: Git:只存储一个文件