Git – remote: error: refusing to update checked out branch

Yaba!!! Made a couple of changes to a Git clone of my project; tried to push the changes to my remote repository and got the errors below.

phiri@PHRLIG001:~/Projects/saldru$ git push laboratory
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 38.96 KiB, done.
Total 4 (delta 1), 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'.
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to ''

Git status clearly shows that my current working tree is ahead of my remote repository by one commit.

phiri@PHRLIG001:~/Projects/saldru$ git status
# On branch master
# Your branch is ahead of 'laboratory/master' by 1 commit.
nothing to commit (working directory clean)

A quick search online reveals that its a very well documented problem with varying solutions [1, 2]. I particularly found the options below helpful and use them in the order they appear. Note that Option #3 is not a recommended best practice, especially if working in a team.

Option #1: Fool Git by using a dummy branch
cd [remote repo]
git check -b [dummy branch name]

Option #2: Create a bare repo
cd [remote repo]
rm -rf *
mv .git/* .
vi config [and change bare option from false to true]

Option #3: Alter receive.denyCurrentBranch configuration variable
cd <remote repo>
git config receive.denyCurrentBranch warn

phiri@PHRLIG001:~/Sandbox/git/repo$ man git config
 If set to true or "refuse", git-receive-pack will deny a ref update to the currently checked out branch of a non-bare
 repository. Such a push is potentially dangerous because it brings the HEAD out of sync with the index and working tree.
 If set to "warn", print a warning of such a push to stderr, but allow the push to proceed. If set to false or "ignore",
 allow such pushes with no message. Defaults to "refuse".

Please comment if you notice an anomaly or if you know of other ways to solve this problem.



Categories: Technical
Tags: , , ,

One Pingback/Trackback