git 只存储一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12420924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:35:50  来源:igfitidea点击:

Stash just a single file

gitgit-stash

提问by EoghanM

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?

上面的虽然行不通。任何替代方案?

回答by Wes Hardaker

I think stash -pis probably the choice you want, but just in case you run into other even more tricky things in the future, remember that:

我认为stash -p这可能是您想要的选择,但以防万一您将来遇到其他更棘手的事情,请记住:

Stashis really just a very simple alternative to the only slightly more complex branchsets. 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.

Stash实际上只是对稍微复杂一些的branch集合的一个非常简单的替代方案。Stash 对于快速移动事物非常有用,但是您可以使用分支完成更复杂的事情,而无需那么麻烦和工作。

# 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 rebaseand/or mergethe tmpbranch. It really isn't thatmuch extra work when you need to do more careful tracking than stash will allow.

去做你想做的事,然后简单地rebase和/或mergetmpbranch。当您需要进行比 stash 允许的更仔细的跟踪时这实际上并没有那么多额外的工作。

回答by sealocal

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 dowant to specify a message, use push.

但是,如果您确实要指定消息,请使用push.

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

Both methods work in git versions 2.13+

这两种方法都适用于 git 版本 2.13+

回答by Benjamin Bannier

You can interactively stash single lines with git stash -p(analogous to git add -p).

您可以使用git stash -p(类似于 )以交互方式隐藏单行git add -p

It doesn't take a filename, but you could just skip other files with duntil you reached the file you want stashed and the stash all changes in there with a.

它不需要文件名,但您可以跳过其他文件,d直到到达您想要隐藏的文件并将所有更改存储在那里a

回答by CharlesB

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 stash save --keep-index暂存除此文件之外的所有内容,并告诉 stash 将索引保留为,从而暂存未暂存的文件:

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

As Dan points out, thefiletostashis the only one to be reset by the stash, but it also stashes the other files, so it's not exactly what you want.

正如丹指出的那样,thefiletostash是唯一一个被隐藏重置的文件,但它也隐藏了其他文件,所以这不是你想要的。

回答by Devesh

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 stash”时实际上是指“放弃更改”(并且不要真正使用 git stash 临时存储它),在这种情况下,您可以使用

git checkout -- <file>

Note that git stash is just a quicker and simple alternative to branching and doing stuff.

请注意, git stash 只是分支和做事的一种更快更简单的替代方法。