无法推送到git仓库

46

这是我迄今为止所做的,并且我会说这个过程在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

1
可能是推送到Git存储库无法工作的重复问题。 - David Z
2
注意:使用Git 2.3.0(2015年2月),将可以推送到非裸库检出的分支!(使用config receive.denyCurrentBranch=updateInstead)请参见下面的答案 - VonC
之前的评论有误,正确的命令是 git config receive.denyCurrentBranch updateInstead(没有等号)。 - VonC
7个回答

91

在服务器端,执行以下操作:

git config receive.denyCurrentBranch ignore

然后你可以将其推送到本地。


5
使用更新版的 Git 时,最好使用选项 updateInstead 而不是 ignore;请参考 VonC 的回答。 - jarauh

54
现在可以将代码推送到非裸库(Git 2.3.0,2015年2月)。并且在推送远程仓库中当前检出的分支时也是可行的!请参见commit 1404bcbJohannes Schindelin (dscho)提交的更改。他为receive-pack添加了另一个选项:receive.denyCurrentBranch。当在工作目录之间进行同步时,通过“push”而不是“pull”更新当前分支可能非常方便,例如,在VM中从内部推送修复或在用户机器上制作修复时(开发人员无法安装ssh守护程序,更不用说知道用户的密码)。此补丁不再需要常见的解决方法-将其推送到临时分支,然后在其他计算机上合并。新选项如下:
updateInstead

根据情况更新工作树,但如果有未提交的更改,则拒绝更新。即:
git config receive.denyCurrentBranch updateInstead

1
这是100%的真实情况,但我很烦“ignore”不再起作用了。他们通常会保持向后兼容性,但我刚刚在我的本地Ubuntu服务器上更新了GIT,它停止工作了。我已经将receive.denyCurrentBranch设置为全局和非全局的“ignore”,但没有起作用。只有在将其更改为“updateInstead”后才重新开始工作。 - Mageician
@BrianVPS 奇怪,更新之前你使用的 Git 版本是什么? - VonC

8

请尝试以下命令:

git config receive.denyCurrentBranch warn

我希望 Git 消息中有这个,这样我就可以复制粘贴了。 - Sridhar Sarnobat

7

正如我在这篇文章中指出的那样 heroku-like git setup? 将工作副本推送到远程仓库有一定危险性,因为推送时未提交的更改不会被包括在内,如果操作不当很容易丢失这些未提交的更改(基本上是工作副本与当前分支 HEAD 不同步)。现在 Git 有一个警告提示 - 更多详细信息请参阅以下链接:

git push to a non-bare repository

建议您的发布仓库应该是一个裸仓库,没有检出的树。使用 "git clone --bare" 选项可以创建裸仓库。


6

由于您在服务器上运行了git init,您创建了一个工作目录,但我认为您实际想要创建一个裸仓库

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

当您想要分享您的项目时,请通过在服务器上运行git init --bare project.git来创建一个裸仓库,然后将其克隆到您的机器上,您就可以向其推送更改。

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


1

客户端和服务器端都需要

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

对我来说没有用。客户端和服务器端的版本都是2.16.2.windows.1

我把这个添加到了\\File-server\projectFolder\.git\config中

[receive]
    denyCurrentBranch = updateInstead

这适用于Windows。不需要使用--bare初始化。


1

VonC的回答很有帮助,但我花了一些时间才意识到对于Windows来说,命令应该是以下形式而没有等号。

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

我的版本是Git for Windows v2.11.1


实际上,= 只是一个打字错误,在 Linux 中也不应该是 =。 - hanshenrik
好的,我已经编辑了我的答案,包括 git config 命令。 - VonC

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