Gitpython git push在推送失败时不会引发异常



这似乎是一个已知的问题,但我正在寻找解决方法。有人知道一个好的变通方法吗?

https://github.com/gitpython-developers/GitPython/issues/621

(Pdb) test = self.repo.remotes.origin.push(refspec=self.base_branch, progress=None, force=False)
2020-07-18 12:06:55,744|WARNING|guest|Error lines received while fetching: error: failed to push some refs to 'git@github.com:test/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
(Pdb)```

由于GitPython v3.1.25,如果推送失败,您可以对push()返回的对象调用raise_if_error()以引发异常。在您的示例中,这将是:

self.repo.remotes.origin.push(refspec=self.base_branch, progress=None, force=False).raise_if_error()

如果推送成功,则对origin/branch的本地引用应与branch处于同一提交中。

您可以将其作为一种变通方法。


唯一不起作用的情况是:如果您触发了git push,而您的本地branch与您对origin/branch的本地引用处于同一点,而branch在实际远程上更新。

也就是说:如果你在没有向分支机构提交任何新内容的情况下进行推送
要100%确定:您可以在推送前检查branch是否处于origin/branch,并采取相应行动(不要推送?(。

最新更新