2

Here's what I've done so far:

I successfully cloned my remote repo to a new directory on my local machine.

Then I edited a file in working copy, committed it, and tried to push it to the remote repo. Here's the error I got:

$ git push origin master
root@gohyperspace.com's password:
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 456 bytes | 0 bytes/s, done.
Total 5 (delta 4), 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 inconsist
ent
remote: error: with what you pushed, and will require 'git reset --hard' to matc
h
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 root@gohyperspace.com:/var/www/html
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'root@gohyperspace.com:/var/www/html'

Do you have any ideas on how I can resolve this? Thanks.

Here's my local Git cofiguration:

$ git config -l
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
merge.tool=tortoisemerge
gui.recentrepo=C:/Users/Chris/Dev/Projects/html
user.email=JazzcatCB@gmail.com
user.name=CBarnhill
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
remote.origin.url=root@gohyperspace.com:var/www/html
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=Chris Barnhill
user.email=JazzcatCB@gmail.com
gui.wmstate=normal
gui.geometry=887x427+26+26 171 192
Chris Barnhill
  • 621
  • 1
  • 7
  • 12
  • Git questions are very welcome on SO :) – StuartLC Jun 24 '13 at 13:40
  • possible duplicate of [Git push error '\[remote rejected\] master -> master (branch is currently checked out)'](http://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked) – kostix Jun 24 '13 at 14:16

2 Answers2

4

Isn't the long description in the message clear enough?

You can do three things:

  • make the original repo bare, and push whatever you like
  • set the receive.denyCurrentBranch in config as suggested to allow pushing to checked-out branches, and take care of the discrepancies
  • check out a different branch in the target repo

possibly even force push would work, I don't suggest that.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
4

The remote master-branch is obviously in a non-bare state, which means, that anyone pushing into this branch would overwrite the existing status of the checked out working copy (the reference to HEAD). This is not a good thing.

To solve this, you either use a bare repository as a "communal" repo (do this with 'git init --bare'), or work with branches:

git checkout -b myBranch

Do your work on this branch and commit. Then push your branch with 'git push origin myBranch' without having to destroy anything. After that, your branch in the remote repo can be merged or rebased into master.

Andreas Gnyp
  • 1,500
  • 1
  • 19
  • 25
  • I created a remote branch named 'CBarnhill' and committed a file change. I tried the following push command, but got the following error: $ git push origin CBarnhill root@gohyperspace.com's password: fatal: 'var/www/html' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. – Chris Barnhill Jun 29 '13 at 14:45
  • Regarding working with a bare remote repo, this isn't an option because the remote repo contains production code. I can't delete it. – Chris Barnhill Jun 29 '13 at 14:49
  • can you please offer me any further advice on my git issue? – Chris Barnhill Jun 30 '13 at 12:43
  • usually you wouldn't use a working directory for production. There are a lot of files that don't belong there, like .gitignore, for example, and you run the risk of overwriting production code meddling with the repo. You can use deployment tools for that, or write a script that deploys your code to production. ... There are surely some guidelines somewhere on the net, but I could also give you a few hints on how to set up a production system – Andreas Gnyp Jun 30 '13 at 12:57
  • I created the repo from the existing production directory on the production server. Did I set this up wrong? My working directory is located on my local computer. I cloned the production directory from the server and am attempting to do development here. This is where I am encountering the push error. – Chris Barnhill Jul 01 '13 at 02:53
  • You are probably not able to push into the repo back, because the origin-url has changed somehow. You attempt to push to "var/www/html". Instead it should be "/var/www/html". Nevertheless, this is NOT how you should do this. If you really want to keep the git-repo as a deployment tool, you need to create a bare-repo somewhere else and clone this one into production (please don't quote me on that ;-)). For example in "/opt/repo.git" on the remote machine. Depending on your rights there, of course. – Andreas Gnyp Jul 01 '13 at 15:03
  • OK. Please help me to understand clearly what you are saying. So I am to create a bare repo and push my local repo to that repo. Then I delete my existing production contents and clone the bare repo into production? So when I need to update production, I would pull the latest from the bare repo? If this is what you intended to say, please confirm. Thanks! – Chris Barnhill Jul 01 '13 at 15:46
  • Yes, this would basically be the way. With this you can "protect" your production code from accidental changes, have a bare repo for exchange and you could also setup a "testing"-ground in this manner. For example some additional (obfuscated) folder - like "yourdomain.com/fallonyourkeyboard" - where you could test your code, before it goes into the production folder. – Andreas Gnyp Jul 01 '13 at 17:44
  • I created a bare repo on my production server. I then pushed my local repo to the bare repo and that worked. However, I then realized that I should have pushed my production repo to bare instead, because my local is outdated. So I tried that but I got an error. So I tried creating a 'production' branch on bare and pushing to that. The problem is that when I do 'git push /var/www/html.git (my bare repo)' it reports that it cannot update 'master'. How do I push to the 'production' branch on bare? – Chris Barnhill Jul 02 '13 at 17:40