253

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?

3

14 Answers 14

302

Reason:You are pushing to a Non-Bare Repository

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.

12
  • 1
    Yes! This is correct thank you! As I have about a million things to do, I accidently cloned the working directory....doh!
    – Funky
    Jun 20, 2012 at 11:10
  • 36
    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. Feb 14, 2013 at 15:18
  • 1
    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). Feb 15, 2013 at 16:50
  • 12
    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
    – VonC
    Feb 1, 2015 at 12:36
  • 1
    @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!
    – Shahbaz
    Jan 23, 2016 at 19:26
148

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.

4
  • 4
    This is the one-liner fix I was looking for.. but maybe explain bare non-bare repos like the @shahbaz answer
    – Mr5o1
    Jan 24, 2016 at 23:24
  • This will allow the change history to be pushed, but those changes won't be reflected in the non-bare repo.
    – jhill515
    Aug 8, 2018 at 21:55
  • will you git config core.bare false and git reset --hard ?
    – aircraft
    Nov 15, 2019 at 14:53
  • 1
    As mentioned above the remote will not be modified when you push to it. Mar 26, 2020 at 15:58
55

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.

1
32

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
1
  • 4
    I prefer this local to the git repo: git config receive.denyCurrentBranch updateInstead
    – klor
    Jan 8, 2020 at 11:24
25

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

$ git config core.bare true
2
  • This does not work. The repository stays empty after pushing into it.
    – Soerendip
    Jul 23, 2018 at 17:17
  • as said by jhil515 above, the non-bare files are not updated, only the databases. Jun 13, 2019 at 8:21
16

As there's already an existing repository, running

git config --bool core.bare true

on the remote repository should suffice

From the core.bare documentation

If true (bare = true), the repository is assumed to be bare with no working directory associated. If this is the case a number of commands that require a working directory will be disabled, such as git-add or git-merge (but you will be able to push to it).

This setting is automatically guessed by git-clone or git-init when the repository is created. By default a repository that ends in "/.git" is assumed to be not bare (bare = false), while all other repositories are assumed to be bare (bare = true).

0
15

TLDR

  1. Pull & push again: git pull &&& git push.
  2. Still a problem? Push into different branch: git push origin master:foo and merge it on remote repo.
  3. Alternatively force the push by adding -f (denyCurrentBranch needs to be ignored).

Basically the error means that your repository is not up-to-date with the remote code (its index and work tree is inconsistent with what you pushed).

Normally you should pull first to get the recent changes and push it again.

If won't help, try pushing into different branch, e.g.:

git push origin master:foo

then merge this branch on the remote repository back with master.

If you changed some past commits intentionally via git rebase and you want to override repo with your changes, you probably want to force the push by adding -f/--force parameter (not recommended if you didn't do rebase). If still won't work, you need to set receive.denyCurrentBranch to ignore on remote as suggested by a git message via:

git config receive.denyCurrentBranch ignore
1
  • Pushing a branch instead of to master worked for me. I had my local server check out a remote git repository and I checked it out from the local server. I'm fine with merging my branch to master on the server and then pushing it. +1 Sep 17, 2020 at 1:39
8

As mentioned in other answers, you can not push to a checked-out branch. Let's start there.

From the remote server, Let's checkout our remote repository to a temporary branch.

git checkout -b temp

Now from our local repository, we can push to the master branch.

git push

But this is not a permanent solution. Future pushes will bring the same problem. To solve this once and for all, you need to turn the remote repository into a bare repository. From the remote server, enter:

git config core.bare true

Now you can push to the remote without any problems.

In future, create your remote repositories using the --bare option like so:

git init --bare
3

Maybe your remote repo is in the branch which you want to push. You can try to checkout another branch in your remote machine. I did this, than these error disappeared, and I pushed success to my remote repo. Notice that I use ssh to connect my own server instead of github.com.

1

I has this error because the git repo was (accidentally) initialised twice on the same location : first as a non-bare repo and shortly after as a bare repo. Because the .git folder remains, git assumes the repository is non-bare. Removing the .git folder and working directory data solved the issue.

1
  • 1
    In my situation, this was the case. Removing the .git folder on the remote allowed me to push the initial commit.
    – tim.rohrer
    Mar 2, 2018 at 1:29
1

It's works for me

  1. git config --global receive.denyCurrentBranch updateInstead

  2. git push origin master

1

I has the same issue and try to do this:

  1. Ssh to gitlab server (remote server machine not your personal machine) and found [core]:bare value is false in yourRepo/.git/config

enter image description here

2. vim config and edit it to true Now,it's fine.I can push to master.

0

I got this error when I was playing around while reading progit. I made a local repository, then fetched it in another repo on the same file system, made an edit and tried to push. After reading NowhereMan's answer, a quick fix was to go to the "remote" directory and temporarily checkout another commit, push from the directory I made changes in, then go back and put the head back on master.

0

In another post's answer explain this problem. In my view, a better way to do is:

  1. For the repository in your remote server: git branch -b work, work is a working branch specially for remote server.
  2. Push your local commits to your remote server and local branch's name (dev) should be different from work to avoid mess up, and now no errors will appear for git push origin dev.
  3. Merge dev to work in your remote server. git merge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.