git run 命令如果没有什么要提交的
这个问题可能既与 bash 有关,也与 git 有关。
如果没有要提交的内容,如何运行命令?我执行了以下操作,但即使没有任何可提交的内容,它也会运行 echo hello
命令:
if git diff --exit-code; then
echo hello
fi
来自 python 背景,我假设会发生的是如果 git ...
> 命令为空,则 if 语句内的内容将不会执行。我已经确认 git diff --exit-code 没有返回任何内容。
This question is probably about bash as much as it is about git.
How do I run a command if there is nothing to commit? I did the following, but it runs the echo hello
command even if there is nothing to commit:
if git diff --exit-code; then
echo hello
fi
Coming from a python background, what I assumed would happen is if git ...
command is empty then the stuff inside the if statement would not execute. I have confirmed that git diff --exit-code
returns nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TLDR:您当前的代码在成功情况下回显 hello,而不是在失败情况下回显。
我们可以通过反转条件来解决这个问题,所以它变成:
我建议看看 如何使用“if”语句检查退出状态,它提供了一些如何执行此操作的选项。
TLDR: Your current code echoes hello in the success case, not the failure case.
We can fix this by inverting the condition, so it becomes:
I'd recommend having a look at How to check the exit status using an 'if' statement which provides a few options for how to do this.
尝试这样的事情:
Try something like this: