我该如何保存/应用一个有名字的git储藏(stash)? 我不想在 git stash list
中查找它的索引号。我尝试了 git stash save "my_stash_name"
,但这只会更改储藏的描述,对应的 git apply "my_stash_name"
是无效的。
保存带有消息的存储:
git stash push -m "my_stash_name"
另外一种选择(自v2.16起已被弃用):
git stash save "my_stash_name"
列出 stash:
git stash list
所有隐藏的内容都存储在堆栈中。
要弹出(即应用并删除)第 n
个存储:
git stash pop stash@{n}
git stash pop
无法按名称弹出(即应用和丢弃)一个存储(请参见脚注1)。
应用第 n
个存储:
git stash apply stash@{n}
按名称应用存储:
git stash apply stash^{/my_stash_name}
注释-1:
请参阅man git-stash
部分中的apply
:
与pop不同,可能是任何看起来像由stash push或stash create创建的提交。
可能的解决方法(在git版本2.27和2.31上测试):
git stash pop $(git stash list --pretty='%gd %s'|grep "my_stash_name"|head -1|gawk '{print $1}')
git stash apply <custom-name>
命令来应用自定义名称。 - stewSquaredstash apply
可能会出现错误,所以记得将其放在引号中:git stash apply "stash@{n}"
。 - Naveengit stash push -m "message"
should be used instead of git stash save
as the latter is deprecated since version 2.15.x/2.16. To retrieve the stash, you can use git stash list
, which will output a list of stashes.
Usage example:
git stash push -m "message"
Here, "message" refers to the note for that particular stash.
stash@{0}: On develop: perf-spike
stash@{1}: On develop: node v10
然后你只需使用apply
,并将stash@{index}
作为参数传递:
git stash apply 1
参考资料 Git存储手册
git stash push
更多信息请查看:https://dev59.com/NFcP5IYBdhLWcg3wP32t#47231547 - VonCgit stash apply stash@{1}
命令时,你可能会收到一个错误提示:error: unknown switch 'e'
。此时,请使用以下两种方法代替:git stash apply --index 1
或者git stash apply 'stash@{1}'
。如果非要使用原来的命令,需要用反引号转义大括号{}
和}
,像这样:git stash apply stash@
“{
”1
“}
”。 - LosManosstash@{1}
这样的奇怪命名方式?这在命令行中很难输入。如果我们可以输入类似于git stash show -p @1
这样的内容,将会更容易… - zerox如果您只是想找一个轻量级的方法来保存部分或全部正在进行的工作副本更改,并在需要时重新应用它们,那么可以考虑使用补丁文件:
# save your working copy changes
git diff > some.patch
# re-apply it later
git apply some.patch
有时我会想是否应该为此使用" stash ",但当我看到上面的疯狂事情时,我对自己正在做的感到满意 :)
git apply --3way some.patch
来增强这个答案。这更类似于传统的 git stash apply
方法。否则,冲突可能会导致补丁应用失败。 - Vance Palaciogit stash
和git diff
中。如果您在暂存之前将它们暂存,那么它们将被包含在内。 - cincodenada如果您认为这很重要,您可以将存储转换为分支:
git stash branch <branchname> [<stash>]
从手册中获取:
此命令创建并检出一个名为<branchname>
的新分支,该分支从创建<stash>
时的提交开始,将记录在<stash>
中的更改应用于新的工作树和索引,如果成功完成,则删除<stash>
。如果没有给出<stash>
,则将应用最新的密藏。
如果您运行了git stash save
命令的分支已经发生足够的更改,导致使用git stash apply失败,那么此命令就会很有用。因为这个密藏被应用在当时HEAD为git stash运行时的提交之上,它可以无冲突地还原最初的存储状态。
您稍后可以将这个新分支重新基于某个其他位置,该位置是您存储时的子孙节点。
git stash apply
一样)的情况下执行此操作并保留存储? - Kasun Siyambalapitiyagit stash push -m 'name'
起作用了。 - wortwart我在我的.zshrc
文件中有这两个函数:
function gitstash() {
git stash push -m "zsh_stash_name_$1"
}
function gitstashapply() {
git stash apply $(git stash list | grep "zsh_stash_name_$1" | cut -d: -f1)
}
用这种方式使用它们:
gitstash nice
gitstashapply nice
stashput = "!f() { git stash push -m "stash_name_$1"; }; f"
.stashget = "!f() { git stash apply $(git stash list | grep 'stash_name_$1' | cut -d: -f1); }; f"
. 然后在你的sh配置文件中(例如.bashrc或.bash_aliases)添加以下内容:
## git stash by name. alias gsp="git stashput " alias gsg="git stashget "
. 现在你就可以在命令行上像这样使用它了:prompt# gsp localchanges
.
prompt# gsg localchanges
. - dyodjigrep -m 1 "zsh_stash_name_$1"
,这样如果同一个名称被多次使用,它就会返回第一个结果:https://dev59.com/j2445IYBdhLWcg3wJ298#5013198 - pgarciacamou所以,我不确定为什么在这个话题上会有那么多的困惑。我可以使用push
和已弃用的save
来命名一个git stash
,并使用正则表达式来使用apply
将其取回:
$ git stash push -m "john-hancock"
$ git stash apply stash^{/john-hancock}
如前所述,save
命令已被弃用,但仍可使用,因此您可以在无法使用push
调用更新旧系统的情况下使用它。与push
命令不同,save
不需要使用-m
开关。
// save is deprecated but still functional
$ git stash save john-hancock
这是 Git 2.2 和 Windows 10。
以下是漂亮的动画 GIF,演示了该过程。
GIF运行速度很快,但是如果你看一下,过程如下:
ls
命令显示目录中有4个文件touch example.html
添加第5个文件git stash push -m "john-hancock" -a
(-a
包括未跟踪的文件)ls
命令在stash之后显示4个文件,这意味着stash和隐式硬重置起作用了git stash apply stash^{/john-hancock}
运行ls
命令列出5个文件,显示example.html文件已经恢复,这意味着git stash apply
命令起作用了。坦白地说,我不确定这种方法的好处是什么。给stash命名确实有价值,但是检索没有。也许对于脚本保留和取消保留过程有帮助,但通过名称弹出stash仍然更容易。
$ git stash pop 3
$ git stash apply 3
对我来说,这比正则表达式看起来容易得多。
git stash push -m "john-hancock" -a
对我没用,相反,使用 git stash push -m "john-hancock" --include-untracked
可以将未跟踪/新添加的文件包含在存储中。干杯。 - SidStash(储藏)并不像你想的那样是一种永久性的东西。你可能最好使用提交时的标签。构建出你希望储藏的内容,将其作为一个提交提交。为该提交创建一个标签。然后将你的分支回滚到HEAD^
。现在当你想要重新应用这个储藏时,可以使用git cherry-pick -n tagname
(-n
是--no-commit
)。
--no-stage
选项!相关链接:https://dev59.com/dlwY5IYBdhLWcg3wq5fl#32333564 - Ciro Santilli OurBigBook.comgit reset @
很容易操作。 - Flip使用git stash push -m aNameForYourStash
命令进行保存。然后使用git stash list
命令查看要应用的储藏编号(index)。最后使用git stash pop --index 0
命令弹出储藏并应用它。
注意: 我正在使用git version 2.21.0.windows.1
这个怎么样?
git stash save stashname
git stash apply stash^{/stashname}
git stash apply stash^{/<regex>}
不起作用(它实际上并没有搜索存储列表,请参见被接受的答案下的评论)。 - Michaelgit stash list
命令,它会显示所有存储的进度,并附带它们的索引号。git stash apply 0
命令,其中的 0 是我从第一个命令中查找到的索引号。$ git stash push -m "say-my-name"
$ git stash apply stash^{/say-my-name}
git stash push -m stashname
是当前语法。git stash save stashname
已被弃用。 - SherylHohmangit bottle name
,然后稍后使用git apply name.patch
,如果您想要更详细的解释和使用示例,请参见我的答案。 - GrayedFox