Git Push error: refusing to update checked out branch

git bare repository
could not update working tree to new head
remote error by default updating the current branch in a non bare repository remote is denied
git remote rejected failed to update ref
error does not have a commit checked out
git init --bare
you cannot update a remote ref that points at a non commit object
bare vs non bare repository

I have solved some merge conflicts, committed then tried to Push my changes and received the following error:

c:\Program Files (x86)\Git\bin\git.exe push --recurse-submodules=check "origin" master:master
Done
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 C:/Development/GIT_Repo/Project
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'C:/Development/GIT_Repo/Project'

Does anyone know what could be causing this error?

There are two types of repositories: bare and non-bare

Bare repositories do not have a working copy and you can push to them. Those are the types of repositories you get in Github! If you want to create a bare repository, you can use

git init --bare

So, in short, you can't push to a non-bare repository (Edit: Well, you can't push to the currently checked out branch of a repository. With a bare repository, you can push to any branch since none are checked out. Although possible, pushing to non-bare repositories is not common). What you can do, is to fetch and merge from the other repository. This is how the pull request that you can see in Github works. You ask them to pull from you, and you don't force-push into them.


Update: Thanks to VonC for pointing this out, in the latest git versions (currently 2.3.0), pushing to the checked out branch of a non-bare repository is possible. Nevertheless, you still cannot push to a dirty working tree, which is not a safe operation anyway.

git push error: "refusing to update checked out branch" · Issue #361 , 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: is denied, because it will make the index and work tree inconsistent remote: with what you pushed, and will require 'git reset --hard' to match remote: the work One other option that supports direct pushing to the checked out repo is using a non-checked out branch to sync. This is the way how git-annex is syncing its file refs. That however would require some additional logic in gopass.

I solved this problem by first verifying the that remote did not have anything checked out (it really was not supposed to), and then made it bare with:

$ git config --bool core.bare true

After that git push worked fine.

Solved: Unable to push to repository, "git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin remote: error: refusing to update checked out branch: refs/heads/master [K. 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.

Summary

You cannot push to the one checked out branch of a repository because it would mess with the user of that repository in a way that will most probably end with loss of data and history. But you can push to any other branch of the same repository.

As bare repositories never have any branch checked out, you can always push to any branch of a bare repository.

Autopsy of the problem

When a branch is checked out, committing will add a new commit with the current branch's head as its parent and move the branch's head to be that new commit.

So

A ← B
    ↑
[HEAD,branch1]

becomes

A ← B ← C
        ↑
    [HEAD,branch1]

But if someone could push to that branch inbetween, the user would get itself in what git calls detached head mode:

A ← B ← X
    ↑   ↑
[HEAD] [branch1]

Now the user is not in branch1 anymore, without having explicitly asked to check out another branch. Worse, the user is now outside any branch, and any new commit will just be dangling:

      [HEAD]
        ↓
        C
      ↙
A ← B ← X
        ↑
       [branch1]

Hypothetically, if at this point, the user checks out another branch, then this dangling commit becomes fair game for Git's garbage collector.

Git Push error: refusing to update checked out branch (Source Control), “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. More modern versions of Git refuse to update a branch via a push if that branch is checked out. The easiest way to work between two non-bare repositories is either to always update the repositories by pull (or fetch and merge) or, if you have to,

cd into the repo/directory that you're pushing into on the remote machine and enter

$ git config core.bare true

Git push error '[remote rejected] master -> master (branch is currently , error: refusing to update checked out branch: refs/heads/master. error: By Now you can delete all the files except .git in that folder. After that, you will be able to perform git push to the remote repository without any errors. remote: error: refusing to update checked out branch: refs/heads/master [K. remote: error: By default, updating the current branch in a non-bare repository [K. remote: error: is denied, because it will make the index and work tree inconsistent [K. remote: error: with what you pushed, and will require 'git reset --hard' to match [K

For me, the following did the trick:

git config --global receive.denyCurrentBranch updateInstead

I set up drive F:, almost in its entirety, to sync between my Windows 10 desktop and my Windows 10 laptop, using Git. I ended up running the above command on both machines.

First I shared the desktop's F drive on the network. Then I was able to clone it on my laptop by running:

F: git clone 'file://///DESKTOP-PC/f'

Unfortunately, all the files ended up under "F:\f\" on my laptop, not under F:\ directly. But I was able to cut and paste them manually. Git still worked from the new location afterwards.

Then I tried making some changes to files on the laptop, committing them, and pushing them back to the desktop. That didn't work until I ran the git config command mentioned above.

Note that I ran all these commands from within Windows PowerShell, on both machines.

UPDATE: I still had issues pushing changes, in some cases. I finally just started pulling changes instead, by running the following on the computer I want to pull the latest commit(s) to:

git pull --all --prune

Unable to Push Changes to Remote, Hi all, I am struggling to push changes from a one repository to another Created a git project called 'git-testing' and initialised the git Total 3 (delta 0), reused 0 (delta 0) remote: error: refusing to update checked out branch:  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

Git – remote: error: refusing to update checked out branch, 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. 1. 2. 3. 4. 5. 6. error: refusing to update checked out branch: refs/heads/master. error: By default, updating the current branch in a non-bare repository. error: is denied, because it will make the index and work tree inconsistent. error: with what you pushed, and will require 'git reset --hard' to match. error: the work tree to HEAD. error:

I'm putting my foot down, He shook his head, "I even checked using the git remote tool and the push is Unpacking objects: 100remote: error: refusing to update checked out branch: 

remote: error: reusing to update checked out branch: refs / heads , When using Git Push code to data warehouse, the following error is remote: error: refusing to update checked out branch: refs/heads/master.

Comments
  • duplicated: stackoverflow.com/questions/2816369/…
  • You actually now have a secure way to push to a non-bare repo with Git 2.3.0 (February 2015) and git config receive.denyCurrentBranch=updateInstead:stackoverflow.com/a/28262104/6309
  • Possible duplicate of Git push error '[remote rejected] master -> master (branch is currently checked out)'
  • Yes! This is correct thank you! As I have about a million things to do, I accidently cloned the working directory....doh!
  • Actually, you can push to a non-bare repository just fine, you just can't push to the one branch that is currently checked out.
  • There are dozens of other scenarios, actually. For example, some of my repositories are just on my workstation and my laptop (not code, but notes I take). On each I have two branches, "workstation" and "laptop". On the workstation, I only check out "workstation", and I only push to the "workstation" branch on the laptop (and vice-versa).
  • You actually now have a secure way to push to a non-bare repo with Git 2.3.0 (February 2015) and git config receive.denyCurrentBranch=updateInstead:stackoverflow.com/a/28262104/6309
  • @skelly Your clone is not bare, while the copy on github. So, while both clones have all the history, the copy on github doesn't have any commit checked out, but your copy does, to allow you to work!
  • This is the one-liner fix I was looking for.. but maybe explain bare non-bare repos like the @shahbaz answer
  • This will allow the change history to be pushed, but those changes won't be reflected in the non-bare repo.