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

3.8k views Asked by At

I'm on mac os x, enabled ssh. I was trying to push local change to remote repo. I simulated remote repo locally like this: Create the remote repo

$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"

OK, everything fine, then in another directory, I did:

$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

It failed and git says:

$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'

Does this indicate that, either remote repo or local client side has any problem? How to fix it?

2

There are 2 answers

2
Mort On BEST ANSWER

The error message is telling you that (the remote you are trying to push to is not a bare repo and in fact that...) the branch you're trying to push to is checked out in that repository. By default it is not allowed to push to a non-bare repo, and not allowed to push to a checked out branch. But you can configure the remote system to allow it by making the git config changes on the remote system that the error message gives you.

I suspect that if you'd googled the error message, you would have found many existing websites giving this same answer.

0
cdutra On

I was getting the same error, the solution was simple:

git push origin HEAD

I just followed the suggestion from the output:

$ 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'.