在Git中使用receive.denyCurrentBranch有什么后果?

use*_*481 42 git git-push git-config git-non-bare-repository

我有一个Git存储库.我克隆了存储库,可以提交我的本地更改.当我将更改推送到服务器时,它可以工作.

一旦我创建了一个分支,我就会检查分支,提交我的工作,然后检查主分支.然后我将我的本地更改合并到主分支中.当我尝试推送到服务器时,我得到以下异常:

Welcome to Git (version 1.7.11-preview20120620)

Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.

$ git push origin master:master
 Counting objects: 9, done.
 Delta compression using up to 4 threads.
 Compressing objects: 100% (7/7), done.
 Writing objects: 100% (8/8), 13.68 KiB, done.
 Total 8 (delta 2), reused 1 (delta 0)
 Unpacking objects: 100% (8/8), done.
 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 c:/jGit
 ! [remote rejected] master -> master (branch is currently checked out)
 error: failed to push some refs to 'c:/gitRepository'
Run Code Online (Sandbox Code Playgroud)

一种解决方案是运行以​​下命令:

git config receive.denyCurrentBranch ignore
Run Code Online (Sandbox Code Playgroud)

在此之后它可以工作,但我想知道为什么我需要使用此选项.这是唯一的选择吗?这样做有什么后果?

我真正想做的是创建分支,将它们合并到主分支中,然后将我的更改推送到服务器.

kan*_*kan 27

您要推送的服务器应该使用裸存储库.

如何将普通的Git存储库转换为裸存储库?

  • 常规git存储库有工作副本(你的源文件)和隐藏的`.git`文件夹与git东西.裸存储库只有git的东西.该错误意味着您正在尝试进入常规存储库,并且其工作副本包含与您推送的分支相同的分支.服务器的git repo不需要工作副本,所以使用裸仓库. (5认同)

小智 24

为什么Git不会让你推送到非裸存储库

原来的海报说:

一种解决方案是运行以​​下命令:

git config receive.denyCurrentBranch ignore
Run Code Online (Sandbox Code Playgroud)

在此之后它可以工作,但我想知道为什么我需要使用此选项.这是唯一的选择吗?这样做有什么后果?

正如我在回答类似问题时所指出的,自Git 1.6.2版以来,Git默认不会让你推送到非裸存储库.这是因为该git push命令仅更新HEAD远程存储库上的分支和引用.什么是这样做也更新非裸远程工作拷贝和舞台区.

因此,当您git status在远程仓库中使用时,您将看到repo的先前状态仍然存在于工作副本中(并且在索引中暂存):

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   previous-state.txt
Run Code Online (Sandbox Code Playgroud)

如果您查看第一次尝试将receive.denyCurrentBranch设置设置为默认refuse值时推送到非裸机远程仓库时收到的错误消息,您将看到该消息告诉您基本相同的事情:

error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error:
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
Run Code Online (Sandbox Code Playgroud)

你应该真的只是推动裸Git存储库

正如指出的一些其他的答案,你真不该推到非纯仓库,因为我上面所指出的原因,和Git的本身告诉你的.

就像这个答案所说的那样,将现有的非裸仓库转换成裸仓库的简单方法就是将其作为一个简单的仓库简单地重新放置:

git clone --bare old-repo
Run Code Online (Sandbox Code Playgroud)

或者你可以尝试搞乱core.bare配置设置,详见答案.

  • 实际上你现在有一种安全的方法可以使用Git 2.3.0(2015年2月)和`git config receive.denyCurrentBranch = updateInstead`推送到非裸机:http://stackoverflow.com/a/28262104/6309 (13认同)
  • @VonC 拼写错误:`git config receive.denyCurrentBranch updateInstead` (2认同)

Fan*_*nky 7

我遇到了同样的错误,并且需要将存储库作为一个开发测试页面在线运行(也就是说,我想,保持一个非裸的回购).希望我通过使用这一系列命令启动存储库来解决它(从git 2.3开始):

git init
git config --global user.email "your@mail.here"
git config --global user.name "Your Name"
git commit
git config receive.denyCurrentBranch updateInstead
Run Code Online (Sandbox Code Playgroud)

如下所示: 无法推入git存储库


Car*_*zel 5

您应该在服务器上有一个裸存储库,而不是带有检出工作树的存储库。Git 告诉你它拒绝覆盖当前在服务器上检出的分支。

有关如何将服务器上的非裸存储库转换为裸存储库的信息,请参阅此答案

  • 您需要在存储库 * 在服务器上 * 上执行此操作。此外,您需要摆脱服务器上已检出的工作树。您是否完成了我链接到的答案中的所有步骤? (2认同)