git stash退出0但未创建任何stash [英] git stash exits 0 but no stash created

查看:123
本文介绍了git stash退出0但未创建任何stash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议我避免使用git pull --autostash ,而应使用:

I've been advised to avoid git pull --autostash, and instead use:

git alias.pull-autostash '!git stash push && git pull --rebase && git stash pop'

当索引或工作树没有变化时,请执行以下操作:

When there are no changes in the index or working tree, doing:

$ git stash push

给予:

No local changes to save

另一个问题是退出状态为0.

An additional problem is that the exit status is 0.

然后,任何stash pop都会pop未被推送的内容.

Any stash pop would then pop something which wasn't pushed.

如何强制创建像git commit --allow-empty这样的空存储区?

How do I force an empty stash to be created, like git commit --allow-empty?

推荐答案

对于脚本编写,请使用git stash create(它将在标准输出上生成已创建存储的哈希ID,如果未创建任何存储,则在标准输出中不生成哈希ID).然后,只要且仅当实际创建一个存储库时,才可以使用git stash store将创建的存储库作为stash@{0}插入.

For scripting, use git stash create (which produces the hash ID of the created stash on standard output, or nothing on standard output if no stash was created). You can then use git stash store to insert the created stash as stash@{0}, if and only if one was actually created.

如果您的Git太旧而无法使用git stash create,则可以在运行git stash save之前和之后在refs/stash上运行两个git rev-parse命令. 1 这些命令可以:

If your Git is too old to have git stash create, you can run two git rev-parse commands on refs/stash before and after running git stash save.1 These can:

  • 都失败了:之前没有隐藏,之后也没有隐藏.
  • 第一个失败,第二个成功:以前没有存储,现在已经存在,因此创建了一个.
  • 两者都成功,两个输出字符串匹配(没有创建存储)或不同(创建了存储).

因此,如果使用--quiet --verify并捕获每个输出的实际输出,则可以判断是否创建了存储.也就是说,此处适用的编程片段为:

Thus, if you use --quiet --verify and capture the actual output from each, you can tell whether a stash was created. That is, the programming fragment that applies here is:

old=$(git rev-parse --quiet --verify refs/stash)
git stash save || die ...
new=$(git rev-parse --quiet --verify refs/stash)
if [ "$old" != "$new" ]; then
    made_stash=true
else
    made_stash=false
fi
... do things ...
if $made_stash; then ... do things with the stash you made ...

(除了在非常有限的特殊情况下,我建议避免同时使用git stash git pull.我对它们有太多不良经验.)

(I recommend avoiding both git stash and git pull except in very limited, specialized circumstances. I've had too many bad experiences with them.)

1 如果您的Git缺少git stash create,它也可能早于git stash push,因此您需要git stash save.

1If your Git lacks git stash create, it probably predates git stash push as well and hence you need git stash save instead.

这篇关于git stash退出0但未创建任何stash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆