Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push fails: `refusing to update checked out branch: refs/heads/master`

Tags:

git

People also ask

How do I push a branch to check out?

The "push" command is used to publish new local commits on a remote server. The source (i.e. which branch the data should be uploaded from) is always the currently checked out HEAD branch. The target (i.e. which branch the data should be uploaded to) can be specified in the command's options.

Which error would you get if you try to push master branch changes to a remote repository?

The error message error: refusing to update checked out branch: refs/heads/master is emitted by the remote repository and it means you're trying to push code to remote non-bare repository that has different code currently checked out in the working directory.

Can not push ref to remote?

A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.

What does git branch command do?

The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands.


Pushing is meant for bare repos. For non-bare repos you should pull into them.

If you want to force this through anyway, you can do as the error message states and set receive.denyCurrentBranch to ignore. SSH to the location of the repo you are pushing to and run:

git config receive.denyCurrentBranch ignore

The master branch at remote site is checked out. If you have access to remote repository, checkout any other branch and then push from your repository.


Create the origin (local) repository as a bare repository (ie. git init --bare), or checkout a branch in there which is not master.


OR,

when you initialize your remote project, using

git init --bare

I managed to get round this by pulling from 'local' in 'current' rather than pushing to 'current' from 'local'.


I know this is quite an old question, but if you go with git init --bare, watch out for 'GIT_DIR=.' which will be set for a bare repo hook if using hooks to checkout the repo after pushing. Use 'export GIT_DIR=.git' in your hook routine to make it recognise the repo you are pulling into....