cannot push into git repository
Asked Answered
git
S

7

47

This is what I have done so far and I will say this procedure worked on Ubuntu 9.10 which perhaps had a different version of git.

server: mkdir ~/git

local: scp -r /../project [email protected]:~/git/
server: cd git
        cd project
        git init 
        git add .
        git commit -a -m "initial"

local: git clone [email protected]:/../git/project /home/name/project
   cd project
   capify .  (from the ruby gem capistrano)
   git add .
   git commit -a -m "capified"
   git push

When I try to push this out I get this error message:

   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 ...
   ! [remote rejected] master -> master (branch is currently checked out)
   error: failed to push some refs to
Souffle answered 11/7, 2010 at 4:28 Comment(5)
possible duplicate of pushing to a git repository does not workMoppet
possible duplicate of What is this Git warning message when pushing changes to a remote repository?Austria
Note: with Git 2.3.0 (February 2015), pushing to a non-bare repo checked out branch will be possible! (with config receive.denyCurrentBranch=updateInstead) See my answer belowPithecanthropus
Correction on the previous comment: it is git config receive.denyCurrentBranch updateInstead (no =)Pithecanthropus
Possible duplicate of What is this Git warning message when pushing changes to a remote repository?Fiertz
R
92

At server side, do this:

git config receive.denyCurrentBranch ignore

Then you can push at local.

Raker answered 22/8, 2012 at 3:24 Comment(1)
With newer versions of git, better use the option updateInstead instead of ignore; see VonC's answer.Scatterbrain
P
55

Pushing to a non-bare repo is now possible (Git 2.3.0 February 2015).
And it is possible when you are pushing the branch currently checked out at the remote repo!

See commit 1404bcb by Johannes Schindelin (dscho):

receive-pack: add another option for receive.denyCurrentBranch

When synchronizing between working directories, it can be handy to update the current branch via 'push' rather than 'pull', e.g. when pushing a fix from inside a VM, or when pushing a fix made on a user's machine (where the developer is not at liberty to install an ssh daemon let alone know the user's password).

The common workaround – pushing into a temporary branch and then merging on the other machine – is no longer necessary with this patch.

The new option is:

updateInstead

Update the working tree accordingly, but refuse to do so if there are any uncommitted changes.

That is:

git config receive.denyCurrentBranch updateInstead
Pithecanthropus answered 1/2, 2015 at 0:2 Comment(2)
This is 100% true, but I'm annoyed that "ignore" no longer works. They usually maintain backwards compatibility, but I just updated GIT on my local Ubuntu server and this stopped working. I had receive.denyCurrentBranch set to "ignore" for global and non-global, didn't work. It only started working again after changing it to "updateInstead".Kingery
@BrianVPS Strange, what version of Git did you have before the update?Pithecanthropus
S
8

Try below command:

git config receive.denyCurrentBranch warn
Schulz answered 21/8, 2018 at 15:20 Comment(1)
I wish the git message had this for me to copy and paste.Lawerencelawes
C
7

As I pointed out in this post heroku-like git setup? pushing to working repositories is a bit dangerous as any work in progress is not taken into account by the push, and it is quite easy to subsequently lose any uncommitted changes (basically the working HEAD can get out of step with the working branch HEAD). Git now has a warning to inform you of this - the, gory, details are in the following link:

git push to a non-bare repository

It is recommended that your published repository should be a bare repo which does not have a checked out tree. Bare repos are created using the "git clone --bare" option.

Cityscape answered 11/7, 2010 at 6:47 Comment(0)
D
6

Since you ran git init on the server you created a working directoy but I think you wanted to make a bare repository instead.

Use a working directory when you want to add, edit and delete files in your project locally on your dev machine.

When you want to share your project, make a bare repository by git init --bare project.git on the server then clone it to your machine and you will be able to push to it.

If you don't want to create a new one now then you can clone your project into a new bare repo by git clone --bare project new-bare-project.git

Dialectology answered 10/1, 2016 at 8:50 Comment(0)
P
1

VonC answer was helpful but it took me some time to realize that for Windows the command would be the following without the equals.

d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

My version is Git for Windows v2.11.1

Protoxide answered 16/2, 2017 at 23:14 Comment(2)
actually the = is just a typo, its not supposed to be = in linux eitherRiegel
OK, I have edited my answer to include the git config command.Pithecanthropus
P
1

Both of client and server side

d:\Repositories\repo.git>git config receive.denyCurrentBranch updateInstead

Didn't work for me.Both of client and server's version are 2.16.2.windows.1

I added this to \\File-server\projectFolder\.git\config

[receive]
    denyCurrentBranch = updateInstead

This works for windows.Don't need init --bare

Propylene answered 4/5, 2018 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.