我已经解决了一些合并冲突,提交,然后尝试推送我的更改,并收到以下错误:

c:\Program Files (x86)\Git\bin\git.exe push --recurse-submodules=check "origin" master:master
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:/Development/GIT_Repo/Project
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'C:/Development/GIT_Repo/Project'

有人知道是什么导致了这个错误吗?


当前回答

这对我很有用

git配置——global receive.denyCurrentBranch updateInstead Git push origin master

其他回答

正如在其他回答中提到的,您不能推到签出的分支。让我们从这里开始。

从远程服务器,让我们将远程存储库签入到一个临时分支。

git checkout -b temp

现在,我们可以从本地存储库推送到主分支。

git push

但这不是长久之计。未来的推动将带来同样的问题。为了一劳永逸地解决这个问题,您需要将远程存储库转换为裸存储库。从远程服务器输入:

git config core.bare true

现在你可以毫无问题地推到遥控器了。

以后,使用——bare选项创建远程存储库,如下所示:

git init --bare

原因:您正在推送到非裸存储库

有两种类型的存储库:裸库和非裸库

裸存储库没有工作副本,您可以将其推入。这些就是你在Github中得到的存储库类型!如果您想创建一个裸存储库,可以使用

git init --bare

因此,简而言之,您不能推入到非裸存储库(编辑:您不能推入到存储库的当前签出分支。使用裸存储库,您可以推送到任何分支,因为没有任何分支被签出。尽管可能,但推入非裸存储库并不常见)。你能做的就是从其他存储库中获取并合并。这就是你在Github中看到的拉请求的工作方式。你让他们离开你,而不是强迫他们。


更新:感谢VonC指出这一点,在最新的git版本(目前是2.3.0)中,可以推送到非裸库的签出分支。然而,您仍然不能推到一个肮脏的工作树,这不是一个安全的操作。

总结

您不能推送到存储库的一个签出分支,因为这会扰乱该存储库的用户,很可能以丢失数据和历史记录而告终。但是您可以推送到同一存储库的任何其他分支。

由于裸存储库从来没有检出过任何分支,所以您总是可以推入裸存储库的任何分支。

问题的解剖

当一个分支签出时,提交将添加一个新的提交,当前分支的头作为其父,并移动该分支的头作为新提交。

So

A ← B
    ↑
[HEAD,branch1]

就变成了

A ← B ← C
        ↑
    [HEAD,branch1]

但如果有人可以推送到中间的那个分支,用户就会进入git所说的分离头部模式:

A ← B ← X
    ↑   ↑
[HEAD] [branch1]

现在用户不再在branch1中,没有显式地要求签出另一个分支。更糟糕的是,用户现在在任何分支之外,任何新的提交都会悬空:

      [HEAD]
        ↓
        C
      ↙
A ← B ← X
        ↑
       [branch1]

假设,如果此时用户签出了另一个分支,那么这个悬空提交将成为Git垃圾收集器的公平游戏。

在另一篇文章中解释这个问题。 在我看来,更好的做法是:

对于远程服务器中的存储库:git branch -b work, work是一个专门用于远程服务器的工作分支。 将本地提交推到远程服务器,本地分支的名称(dev)应该与work不同,以避免混乱,现在git Push origin dev不会出现错误。 合并开发以在远程服务器中工作。git合并。

我也有同样的问题,我试着这样做:

Ssh到gitlab服务器(远程服务器机器而不是你的个人机器),发现[core]:bare value is false in yourRepo/.git/config

2. Vim配置并将其编辑为true 现在,它很好。我可以推到大师。