使用 git + post-receive hook 管理网站:推送更改时出错 [英] Managing website using git + post-receive hook: error pushing changes

查看:28
本文介绍了使用 git + post-receive hook 管理网站:推送更改时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如果工作树不是空的,我如何将本地提交推送到我的开发服务器?

Question: How do I push my local commits to my development server if that worktree is not bare?

我的情况:

  1. 我在我的开发服务器上设置了 git,这样当我推送本地更改时,它们会被添加到一个分离的工作树中(如 此处描述这篇文章很像).
  2. 然后我在开发服务器上运行 git 时遇到问题(例如,git status),因为 git 找不到工作树.
  3. 我询问了 SO 和 得到提示,我需要在我的配置文件中设置 bare = false 并指定工作树.甜.
  1. I set up git on my development server so that when I push local changes, they are added to a detached working tree (as described here and much like this post).
  2. I then experienced problems running git on the dev server (eg, git status) because git could not find the working tree.
  3. I asked around SO and got the tip off that I needed to set bare = false in my config file and specify the worktree. Sweet.

但是现在当我尝试将提交推送到开发服务器时,我收到此错误:

But now when I try to push my commits to the dev server I get this error:

$ git push origin master
xxxx@xxx.xxx.xxx.xxx's password:
Counting objects: 26, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 2.56 KiB, done.
Total 18 (delta 8), reused 0 (delta 0)

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'.

To ssh://xxxx@xxx.xxx.xxx.xxx/xxx/xxx/xxxxxx.git
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://xxxx@xxx.xxx.xxx.xxx/xxx/xxx/xxxxxx.git'

我看到 这个其他用户有同样的问题.不幸的是,对他有用的解决方案是在他的配置中设置 bare = true,这会重现我在上面第 3 步中遇到的问题.

I see that this other user had the same problem. Unfortunately, the solution that worked for him was setting bare = true in his config, which would recreate the problem I was having in step 3 above.

错误信息说

您可以将 'receive.denyCurrentBranch' 配置变量设置为远程:错误:忽略"或警告"

You can set 'receive.denyCurrentBranch' configuration variable to remote: error: 'ignore' or 'warn'

就我而言,我可以这样做吗?这对我来说听起来不错,但我对 git 还不够熟悉,还没有意识到一个坏主意出现...

In my case, is it ok for me to do this? It sounds good to me, but I'm not familiar enough with git yet to recognize a bad idea when it comes around...

编辑

需要说明的是,我已经添加了一个 post-receive 钩子并且它运行顺利.问题是我似乎必须在 bare=true(并且能够推送到开发服务器)和 bare=false(并且能够运行 git开发服务器上的命令).

Just to be clear, I have already added a post-receive hook and it's working smoothly. The problem is that I seem to have to choose between bare=true (and being able to push to the dev server) and bare=false (and being able to run git commands on the dev server).

推荐答案

我已经找到了一个可以接受但不是很理想的解决方案.基本上,当我需要将开发更改推送到我的实时服务器时,我只是通过编辑我的配置文件来规避这个问题.

I've worked out a solution that is acceptable, but not quite ideal. Basically, I am just circumventing the problem by editing my config file when I need to push dev changes to my live server.

允许开发人员将本地更改推送到开发人员的配置文件:

Config file which allows developers to push local changes to dev:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = true

推送开发更改时的配置文件:

Config file when pushing dev changes to live:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        worktree = /var/www/example.com/httpd/

优点

  1. 作品
  2. 一旦我将配置文件更改为推送上线,repo 就被锁定",其他开发人员无法推送更改
  3. 不是什么大问题,因为无论如何我都必须登录 dev 来处理数据库迁移

缺点:基本上只是我确信可以自动化的工作,如果我知道如何正确地做的话.

Cons: basically just that it's work that I'm sure could be automated, if I knew how to do it correctly.

注意:我喜欢@vonC 的建议,即设置 post-receive hook 以有条件地也推送到实时服务器.对 post-receve 钩子进行更改很简单,但我不明白钩子会响应的触发器是什么.不过我很想听听建议:)

Note: I like @vonC's suggestion of setting up the post-recieve hook to conditionally also push to the live server. Making the changes to the post-recieve hook is simple enough, but I do not understand what the trigger would be that the hook would respond to. I'd love to hear suggestions though : )

这篇关于使用 git + post-receive hook 管理网站:推送更改时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆