git 如果远程历史记录改变,推送失败

git push fails if history changes in remote

我有脚本 运行 一些 git 命令并尝试推送更改。这些更改不是相互冲突的更改,因为每个更改都适用于不同的文件。

现在发生的情况是,当一个脚本拉取并尝试推送时。在那一瞬间,如果历史发生变化,它就无法推送。

如果推送失败,如何确定?它变基或只是修补代码更改?

我不想强制推送,因为那样会重写历史,而且我会丢失更改。

注意:这些都在同一个分支上工作。

i think i need NFS or something to make sure, commands run while taking a lock

实际上,自 Git 2.23(2019 年第 3 季度)以来,, which reinforces what was already a 之前。

因此只要服务器设置为拒绝任何发散推送(除非使用 --force),如评论所述,git pull --rebase 就足够了。

# repeat until git push works:
git pull --rebase
git push --force      # which is quasi atomic

注:go-git does not support rebase, so you might need to wrap that command (git pull --rebase) in a exec.Command call, using go-git/go-git plumbing/transport/file/client.go.


OP Tilak Raj proposes in :

retry 5 git update-ref -d refs/remotes/origin/main && git pull --rebase=false --no-edit -X ours origin "refs/heads/$BRANCH" && git push -u origin "$BRANCH" &> /tmp/gitplan