tags:

views:

4565

answers:

6

Hi,

Yesterday, I post a question regarding how to clone a git repository from 1 of my machine to another. http://stackoverflow.com/questions/2808177/how-can-i-git-clone-from-another-machine/2809612#2809612

I am able to successfully clone a git repository from my src (192.168.1.2) to my dest (192.168.1.1).

But when I did an edit to a file and then do a 'git commit -a -m "test"' and then do a git push.

I get this error on my dest (192.168.1.1):

git push                                                
[email protected]'s password: 
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
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: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://[email protected]/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://[email protected]/media/LINUXDATA/working'

I have 2 version of git, will that causes this problem?

I have git 1.7 on 192.168.1.2 (src) but git 1.5 on 192.168.1.1 (dest).

I appreciate if someone can help me with this.

Thank you.

+1  A: 

You should only be pushing to a bare repository. A bare repository is a repository that has no checked out branches. If you were to cd to a bare repository directory, you'd only see the contents of a .git directory.

RibaldEddie
There's nothing wrong with pushing to a non-checked out branch in a non-bare repository. This is a perfectly valid way of working.
Charles Bailey
Fair enough, that would work. But that is not what the user is doing.
RibaldEddie
It's not the fact that he isn't using a bare repository that is 'wrong'; it is the fact that he is pushing to a checked out branch. There is no evidence that he has or wants a separate bare repository so your blanket statement that he should only be pushing to a non-bare repository is not giving the asker all the options; one of which might more easily solve his immediate problem.
Charles Bailey
+4  A: 

The error message describes what has happened. 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, by pushing to a separate branch (an import branch) and then merging that branch into the master branch on the remote machine.

The reason for this restriction is that the push operation operates only on the remote git repository it doesn't have access to the index and working tree so, if allowed, a push on the checked out branch would change the HEAD to be inconsistent with the index and working tree on the remote repository.

This would make it very easy to accidentally commit a change that undoes all of the pushed changes and also makes it very difficult to distinguish between any local changes that have not been committed and differences between the new HEAD, the index and the working tree that have been caused by push moving HEAD.

Charles Bailey
Thanks. So how can I fix my problem?In my 192 box, i did '$ cd (project-directory)$ git init$ (add some files)$ git add .'and then in my 191 box, I did a 'git clone ' and edit some files and than try to 'git push'.
hap497
Well, I described the possibilities in my answer. Either you can go to the 192 box and fetch from the 191 box (you might want to add the 191 box as a named remote - look at `git remote add box191 <191url>` ), or you can push from the 191 box to an alternatively named branch (e.g. `git push origin master:refs/heads/upload` ), then to the 192 box and merge (e.g. `git merge upload` ).
Charles Bailey
just adding a thanks for the good explanation
jcinacio
+8  A: 

Just had the same error while I began learning git. The above answers are clearly not for newbies!!! (Going to use non technical terms to get the idea across) Anyways what is happening is that you have 2 repositories, one is the original you first made, and the other the work one you just made. Right now you are in your work repository, and using the "master" branch. But you also happen to be "logged in" in your original repository to the same "master" branch. Now since you're "logged in" in the original Git fears you might mess up because you might be working on the original and screw things up. So what you need to do is return to the original repository and do a "git checkout someotherbranch", now you can push with no problems.

hope this helps

Robert Gould
+18  A: 

You can simply convert your remote repository to bare repository ( There is no working copy in the bare repository - the folder contains only the actual repository data ) .

Execute following command in your remote repository folder:

git config --bool core.bare true

Then delete all the files except .git in that folder. and then you will be able to perform git push to the remote repository without any errors.

John
Informative and it worked great stuff! +1
xenon
Thanks. I also needed this. I was following the submodules tutorial from the [Git Community Book](http://book.git-scm.com/5_submodules.html) and hit this roadblock.
Shiki
A: 

.git repositories should be bare after git init and only become nonbare after git add whatoever or git checkout

fix the git!

Anton
A: 

Thanks, this helped me out a lot. So is the 'correct' thing to do either clone or set your remote to a non-checked out branch? And then sync this with master?

fijiaaron