通过git push模拟git pull --rebase

3

我有一台名为production的服务器,从主分支检出了git源代码并运行。
此外,我还有一个被克隆了git仓库的development服务器,在这里进行代码修改。
我想要将源代码从development->production同步。
我的初始想法是在production服务器上运行git pull --rebase以获取最新提交的文件。然而,由于网络限制(防火墙等),production无法与development通信,但反过来可以: development可以从production进行git pull/push操作。
当我尝试推送时,我收到了以下消息:

+ 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之外,还有其他方法吗?


1
将代码推送到生产服务器的另一个分支,然后通过ssh进入该分支并使用git pull OTHER_BRANCH命令。 - Lasse V. Karlsen
也许是 git push -f origin master - maciejwww
我有点困惑你的设置,为什么要尝试从开发服务器拉取/推送代码?你的主要(可能是裸的)代码仓库在哪里充当中央主机?如果你没有这个,我建议你创建一个(根据你的设置猜测,你应该在生产环境上创建),然后开发人员可以推送/拉取它,生产环境可以从中拉取。 - Nick
@nick - 我不想让我的问题变得复杂,但我的实际设置更为复杂。我所称的 development 服务器实际上是一个代理,它从主裸仓库拉取。我主要的问题是 production 有一个网络限制,防止它从其他服务器拉取。 - oshai
1
明白了。您仍然可以在生产环境中设置一个裸仓库(Git旨在以这种方式分布,因此不会有问题),根据您的设置,这应该可以工作? - Nick
显示剩余3条评论
1个回答

0

评论中提出了几个建议:

  1. 将代码推送到 production 上的另一个分支,在 production 机器上执行 git pull . BRANCH_NAME
  2. production 机器上设置额外的裸仓库。由于 Git 是分布式的,服务器可以在任何一台机器上,对于我的情况,由于网络限制,将其放在 production 上非常简单。

最终我选择了实现第一种方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接