Git 在我的 mac 上推送到 refs/heads/master 失败,如何解决?

Git push to refs/heads/master fails on my mac, how to fix?

我在 mac os x 上,启用了 ssh。我试图将本地更改推送到远程仓库。我在本地模拟远程仓库是这样的:创建远程仓库

$cd ~/learn
$mkdir gittest
$cd gittest/
$git init
$git config --global user.name "username"
$git config --global user.email "useremail"
$touch readme
$git add readme
$git commit -m "empty"

好的,一切正常,然后在另一个目录中,我做了:

$cd ~/learn/client
$git clone trosky@localhost:/Users/trosky/learn/gittest
$cd gittest
$vi readme(add one line)
$git add .
$git commit -m "add line"
$git push origin master:refs/heads/master

它失败了,git 说:

$git push origin master:refs/heads/master
Total 0 (delta 0), reused 0 (delta 0)
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 x@localhost:/Users/trosky/gittest
 ! [remote rejected] master -> master (branch is currently checked out)
 error: failed to push some refs to 'x@localhost:/Users/trosky/learn/gittest'

这是否表明远程仓库或本地客户端有问题?如何解决?

错误消息告诉您(您尝试推送到的远程不是裸仓库,事实上...)您尝试推送到的分支已在该仓库中签出。默认情况下,不允许推送到非裸仓库,也不允许推送到签出的分支。但是您可以通过在错误消息给您的远程系统上进行 git config 更改来配置远程系统以允许它。

我怀疑如果您用谷歌搜索错误消息,您会发现许多现有网站都给出了相同的答案。

我遇到了同样的错误,解决方法很简单:

git push origin HEAD

我只是遵循了输出中的建议:

$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:master

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.