我有一个名为 production
的服务器,其中包含从 master 分支 checkout 并执行的 git 源代码。
此外,我还有一个 development
服务器,其中克隆了我进行代码修改的 git 存储库。
我想同步源代码development -> production
。
我最初的想法是在 production
服务器上运行 git pull --rebase
以获取最新的提交文件。但是,由于网络限制(防火墙等),production
无法联系 development
但反过来也行得通:development
可以 git pull/push
来自 production
.
当我尝试推送时,我收到以下消息:
+ git push origin master
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
我的理解是,为了推送更改,我应该配置 receive.denyCurrentBranch
,然后在推送后在生产主分支上执行 git reset --hard
。这还将覆盖生产中未提交的更改。
有更安全的方法吗?例如,有时有人可能会 ssh 到生产机器并直接更改那里的代码。我更希望我的命令在这种情况下会失败。另一个例子是生产服务器有一些我不敢覆盖/删除的未跟踪文件。
除了生产服务器上的 git reset --hard
之外,还有其他方法吗?
最佳答案
评论中提到的一些建议:
production
上的另一个分支,git pull。 生产
机器上的 BRANCH_NAME
。production
机器上设置额外的裸仓库。由于 git 是分布式的,服务器可以在任何机器上,对于我的网络限制情况,将它放在 production
上很简单。我最终实现了#1。
https://stackoverflow.com/questions/63801514/