如何在没有错误的情况下 git 不提交任何内容?

新手上路,请多包涵

我正在尝试编写一个执行 git commit 的结构脚本;但是,如果没有要提交的内容,git 将以 1 的状态退出。部署脚本将其视为不成功并退出。我确实想检测 实际 的提交失败,所以我不能只为 fabric 忽略 git commit 失败。我如何允许忽略空提交失败,以便部署可以继续,但仍然捕获真正提交失败时导致的错误?

 def commit():
    local("git add -p && git commit")

原文由 kojiro 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 385
2 个回答

通过检查 git diff-index 的退出代码来预先捕捉到这种情况?

例如(在外壳中):

 git add -A
git diff-index --quiet HEAD || git commit -m 'bla'

编辑:根据 Holger 的评论修复 git diff 命令。

原文由 Tobi 发布,翻译遵循 CC BY-SA 4.0 许可协议

来自 git commit 手册页:

 --allow-empty
    Usually recording a commit that has the exact same tree as its
    sole parent commit is a mistake, and the command prevents you
    from making such a commit. This option bypasses the safety, and
    is primarily for use by foreign SCM interface scripts.

原文由 Sven Marnach 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题