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

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'

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


当前回答

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

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

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

其他回答

TLDR

又拉又推:少不要脸的拉,少不要脸的推。 还是个问题吗?Push到不同的分支:git Push origin master:foo并在远程repo上合并它。 或者通过添加-f强制推送(denyCurrentBranch需要被忽略)。


基本上,这个错误意味着您的存储库与远程代码没有更新(它的索引和工作树与您推送的内容不一致)。

通常情况下,您应该首先拉取最近的更改,然后再推它。

如果没有帮助,尝试推到不同的分支,例如:

git push origin master:foo

然后将远程存储库上的这个分支合并回master。

如果你故意通过git rebase改变了一些过去的提交,并且你想用你的更改覆盖repo,你可能想通过添加-f/——force参数来强制推送(如果你没有做rebase,不建议使用)。如果仍然不起作用,你需要设置receive.denyCurrentBranch来忽略远程的git消息:

git config receive.denyCurrentBranch ignore

我解决了这个问题,首先验证该遥控器没有任何检出(它真的不应该),然后使它裸露:

$ git config --bool core.bare true

在那之后,git推得很好。

这对我很有用

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

对我来说,以下几点很管用:

git config --global receive.denyCurrentBranch updateInstead

我设置了F:驱动器,几乎是整个驱动器,用Git在我的Windows 10台式机和Windows 10笔记本电脑之间同步。最后我在两台机器上都运行了上面的命令。

首先,我在网络上共享了桌面的F驱动器。然后我可以在我的笔记本电脑上运行:

F:
git clone 'file://///DESKTOP-PC/f'

不幸的是,所有的文件最终都在我笔记本电脑的“F:\ F”下,而不是直接在F:\下。但我可以手动剪切粘贴。之后Git仍然在新位置工作。

然后我尝试对笔记本电脑上的文件做一些更改,提交它们,并将它们推回到桌面上。直到我运行上面提到的git config命令,这才起作用。

注意,我在两台机器上的Windows PowerShell中运行了所有这些命令。

更新:在某些情况下,我仍然在推动更改方面遇到问题。我终于开始拉更改,通过在计算机上运行以下命令,我想拉最近的提交:

git pull --all --prune

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

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

git checkout -b temp

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

git push

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

git config core.bare true

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

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

git init --bare