221

是否有一个git stash命令可以隐藏您的更改,但也将它们保留在工作目录中?所以基本上git stash; git stash apply一步到位?

4

5 回答 5

214

对于它的价值,另一种方法是暂存您想要保留的更改,然后使用以下命令存储所有内容--keep-index

$ git add modified-file.txt
$ git stash push --keep-index

上面的命令将存储所有内容,但会将文件保留在您的工作目录中。

来自或来自git-scm的官方 Linux Kernel Git 文档git stash

如果--keep-index使用该选项,则已添加到索引的所有更改都将保持不变。

于 2013-07-25T03:31:10.217 回答
79

git stash然后git stash apply( git stash && git stash apply) 将存储文件并在其后立即应用存储。因此,毕竟您将在 stash 和工作目录中进行更改。

如果您想要一个别名,您可以创建一个别名。把这样的东西放到~/.gitconfig

[alias]
    sta = "!git stash && git stash apply"

这种方法的缺点是所有文件都被隐藏并重新创建。这意味着相关文件的时间戳将被更改。(如果我在git sta执行make.

于 2013-07-24T20:08:23.443 回答
13

答案中的一个小改进,实际上可能会使用。

$ git add modified-file.txt  
(OR $ git add .    ---- for all modified file)
$ git stash save --keep-index "Your Comment"
于 2016-06-01T09:43:52.610 回答
11

您可以使用git stash create创建存储提交,然后使用以下命令将其保存到存储中git stash store

git stash store $(git stash create) -m "Stash commit message"

这可以保存到 git 别名中以使其更方便:

git config --global alias.stash-keep '!git stash store $(git stash create)'

git stash-keep -m "Stash commit message"

请注意,这并不能做所有的事情git stash push。例如,它不会将分支名称附加到提交,例如“ stash@{0}: On myBranch: Stash commit message”。

于 2020-03-06T03:40:13.307 回答
4

有一个技巧可以帮助你,而不是隐藏的东西,而是 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)

所以现在你有一个提交标记的存储供你使用,git stash pop无论如何都不可能做,但你可以从那里做一些事情,比如创建补丁或重置文件等,你的工作目录文件也完好无损。

于 2015-04-22T00:42:09.133 回答