【问题标题】:cannot push into git repository无法推送到 git 存储库
【发布时间】:2011-03-14 09:44:49
【问题描述】:

这是我到目前为止所做的,我会说这个过程在 Ubuntu 9.10 上运行,它可能有不同版本的 git。

server: mkdir ~/git

local: scp -r /../project name@url.com:~/git/
server: cd git
        cd project
        git init 
        git add .
        git commit -a -m "initial"

local: git clone name@url.com:/../git/project /home/name/project
   cd project
   capify .  (from the ruby gem capistrano)
   git add .
   git commit -a -m "capified"
   git push

当我尝试将其推出时,我收到以下错误消息:

   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 ...
   ! [remote rejected] master -> master (branch is currently checked out)
   error: failed to push some refs to

【问题讨论】:

标签: git


【解决方案1】:

在服务器端,这样做:

git config receive.denyCurrentBranch ignore

然后你可以在本地推送。

【讨论】:

  • 对于较新版本的 git,最好使用选项updateInstead 而不是ignore;见 VonC 的回答。
【解决方案2】:

现在可以推送到非裸仓库(Git 2.3.0 2015 年 2 月)。
当您推送当前在远程仓库中签出的分支时,这是可能的!

commit 1404bcbJohannes Schindelin (dscho)

receive-pack:为receive.denyCurrentBranch 添加另一个选项

在工作目录之间同步时,可以很方便地通过“push”而不是“pull”来更新当前分支,例如从 VM 内部推送修复程序时,或推送在用户机器上进行的修复程序时(开发人员无权安装 ssh 守护程序,更不用说知道用户的密码了)。

这个补丁不再需要常见的解决方法——推入一个临时分支,然后合并到另一台机器上。

新选项是:

updateInstead

相应地更新工作树,但如果有任何未提交的更改,则拒绝这样做。

即:

git config receive.denyCurrentBranch updateInstead

【讨论】:

  • 这是 100% 正确的,但我很生气“忽略”不再起作用。它们通常保持向后兼容性,但我刚刚在本地 Ubuntu 服务器上更新了 GIT,这停止了工作。我将receive.denyCurrentBranch 设置为全局和非全局的“忽略”,但没有用。将其更改为“updateInstead”后才重新开始工作。
  • @BrianVPS 奇怪,更新前你的 Git 是什么版本的?
【解决方案3】:

试试下面的命令:

git config receive.denyCurrentBranch warn

【讨论】:

  • 我希望 git 消息有这个供我复制和粘贴。
【解决方案4】:

正如我在这篇文章中指出的 heroku-like git setup? 推送到工作存储库有点危险,因为推送不会考虑任何正在进行的工作,并且随后很容易丢失任何未提交的更改(基本上工作 HEAD 可能与工作分支 HEAD 脱节)。 Git 现在有一个警告通知你 - 血腥的细节在以下链接中:

git push to a non-bare repository

建议您发布的存储库应该是没有签出树的裸存储库。裸仓库是使用“git clone --bare”选项创建的。

【讨论】:

    【解决方案5】:

    由于您在服务器上运行git init,因此您创建了一个工作目录,但我认为您想创建一个裸存储库

    当您想在开发机器上本地添加、编辑和删除项目中的文件时,请使用工作目录。

    当你想分享你的项目时,通过git init --bare project.git 在服务器上创建一个裸存储库,然后将其克隆到你的机器上,你就可以推送到它。

    如果你现在不想创建一个新的,那么你可以通过git clone --bare project new-bare-project.git将你的项目克隆到一个新的裸仓库中

    【讨论】:

      【解决方案6】:

      VonC 的回答很有帮助,但我花了一些时间才意识到对于 Windows,命令将是以下不带等号的命令。

      d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead
      

      我的版本是 Git for Windows v2.11.1

      【讨论】:

      • 其实=只是一个错字,在linux中也不应该是=
      • 好的,我已经编辑了答案以包含git config 命令。
      【解决方案7】:

      客户端和服务器端

      d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

      对我不起作用。客户端和服务器的版本都是 2.16.2.windows.1

      我将此添加到 \\File-server\projectFolder\.git\config

      [receive]
          denyCurrentBranch = updateInstead
      

      这适用于 windows。不需要 init --bare

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-15
        • 2018-06-24
        • 2013-07-04
        • 2010-10-25
        • 2013-04-11
        相关资源
        最近更新 更多