存储更改,同时将更改保留在 Git 的工作目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17843384/
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
Stash changes while keeping the changes in the working directory in Git
提问by Michael Dorst
Is there a git stash
command that stashes your changes, but keeps them in the working directory too? So basically a git stash; git stash apply
in one step?
是否有一个git stash
命令可以隐藏您的更改,但也将它们保留在工作目录中?所以基本上是git stash; git stash apply
一步?
回答by
For what it's worth, another way to do this is to stage the changes you want to keep, and then stash everything using --keep-index
:
对于它的价值,另一种方法是暂存您想要保留的更改,然后使用--keep-index
以下方法隐藏所有内容:
$ git add modified-file.txt
$ git stash save --keep-index
The commands above will stash everything including modified-file.txt
, but it will also leave that file staged and in your working directory.
上面的命令将存储包括 在内的所有内容modified-file.txt
,但它也会将该文件保留在您的工作目录中。
From the official Linux Kernel Git documentation for git stash
:
来自官方 Linux 内核 Git 文档git stash
:
If the
--keep-index
option is used, all changes already added to the index are left intact.
如果使用该
--keep-index
选项,则已添加到索引的所有更改将保持不变。
回答by madhead
git stash
and then git stash apply
(git stash && git stash apply
) will stash files and apply stash immediately after it. So after all you will have your changes in stash and in working dir.
git stash
然后git stash apply
( git stash && git stash apply
) 将 stash 文件并在它之后立即应用 stash。因此,毕竟您将在 stash 和工作目录中进行更改。
You can create an alias if you want it in one piece. Just put something like this to ~/.gitconfig
:
如果您想要一个别名,您可以创建一个别名。把这样的东西放在~/.gitconfig
:
[alias]
sta = "!git stash && git stash apply"
The drawback of this approach is that all files are stashed and recreated. This means that timestamps on the files in question will be changed. (Causing Emacs to complain when I try to save the file if opened it before I did the git sta
, and may cause unnecessary rebuilds if you're using make
or friends.)
这种方法的缺点是所有文件都被隐藏并重新创建。这意味着相关文件的时间戳将被更改。(导致Emacs的抱怨时,我尝试保存该文件,如果打开它,我做了之前git sta
,并可能导致不必要的重建,如果你正在使用make
或朋友。)
回答by Premchandra Singh
A small enhancement in the answer which in practical may likely to use.
答案中的一个小改进,实际上可能会使用。
$ git add modified-file.txt
(OR $ git add . ---- for all modified file)
$ git stash save --keep-index "Your Comment"
回答by KenIchi
There's a trick may help you, not stash' thing but FWIW:
有一个技巧可能会帮助你,不是藏起来的东西,而是 FWIW:
git add -A
git commit -m "this is what's called stashing" (create new stash commit)
git tag stash (mark the commit with 'stash' tag)
git reset HEAD~ (Now go back to where you've left with your working dir intact)
And so now you have a commit tagged stash at your disposal, it's not possible to do a git stash pop
anyway but you can do things like creating patch or resetting files etc. from there, your working dir files are also left intact BTW.
因此,现在您有一个带有提交标记的存储供您使用,git stash pop
无论如何都无法执行此操作,但是您可以从那里执行诸如创建补丁或重置文件等操作,您的工作目录文件也保持不变,顺便说一句。
回答by M. Justin
You can use git stash create
to create a stash commit, and then save it to the stash using git stash store
:
您可以使用git stash create
创建存储提交,然后使用git stash store
以下命令将其保存到存储:
git stash store $(git stash create) -m "Stash commit message"
This can be saved to a git alias to make it more convenient:
这可以保存到 git 别名以使其更方便:
git config --global alias.stash-keep-all '!git stash store $(git stash create)'
git stash-keep-all -m "Stash commit message"
Note that this does not do everythingthat git stash push
does. For instance, it does not append the branch name to the commit, e.g. "stash@{0}: On myBranch: Stash commit message
".
请注意,这并不做一切该git stash push
做。例如,它不会将分支名称附加到提交,例如“ stash@{0}: On myBranch: Stash commit message
”。