Recently, I cloned a repository on my machine and made some changes to it. As I was pushing my changes back to the original repo, push failed with the error message:
“refusing to update checked out branch: refs/heads/master. By default, updating the current branch in a non-bare repository is denied…”
There are two things to note here in the error message. First the original repo is non-bare, secondly the push is to a branch which is currently checked out. Following are two possible ways to overcome the issue relating to bare repos and currently checked out branch.
Set Repository as ‘Bare’
Bare repositories do not have any working copy. Changes are not done directly to these repositories, rather changes are pushed from clones. Repositories hosted at GitHub are of this type.
One way to resolve the above issue is to make the repository ‘bare’. This can be done by running below command in original repo folder:git config –bool core.bare true
Change the checked out branch
If you don’t want to convert your original repository to bare, another option is change the current branch. This can be done by:git checkout
In case, there is only one branch in the repository, a temporary branch could be created and checked out:git checkout -b temp
Once the push is executed, run the following commands to bring things back to original state:git checkout master
git branch -d temp
Above will delete the temporary branch and checkout the master branch.