Pergunta

I'm was working with submodules and couldn't get them to work, so I figured I'd just try something simple, that absolutely can't fail. It does. And the error message, to me sounds like someone saying: By default you can't push into a non-empty repository because we will screw up the repo and your working directory. I can't imagine what could be wrong

I ran this simple script to create two Repos. 'Main' with a single file, V, which contains 'Version 1', and Clone that is a clone of Main. It then changes V to be 'Version 2' and commits the change. It then tries to push it and it blows chunks with an error message from heck.

mkdir Main
cd Main
git init
echo Version 1 > V
git add V
git commit -m "Initial"
mkdir ../Clone
cd ../Clone
git clone ../Main .
echo Version 2 > V
git commit -m "V2" -a
git push

Output is:

Initialized empty Git repository in /private/tmp/T/X/Main/.git/
[master (root-commit) a94775b] Initial
 1 file changed, 1 insertion(+)
 create mode 100644 V
Cloning into '.'...
done.
[master 385cf21] V2
 1 file changed, 1 insertion(+), 1 deletion(-)
Counting objects: 5, done.
Writing objects: 100% (3/3), 237 bytes, done.
Total 3 (delta 0), 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 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 /tmp/T/X/Clone/../Main
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/tmp/T/X/Clone/../Main'

Seriously? Updating the current branch is denied? Riiggghhht... And what exactly is the point of version control?

I realize that git is used by people, and some people think it is good enough to even promote. So, clearly the entire way I'm doing something must be wrong, but I can't for the life of me see what could be wrong with this.

Everything I do doesn't work like I would expect.

Foi útil?

Solução

git push is not designed to update the work tree. git push will update the index of the remote but not the work tree. So if you push to the branch that is the current branch of the work tree, the work tree will be out of sync. That can be confusing. If Git let's you push anyway, you may think that the work tree will be updated but it won't be. So to save you that pain it won't let you push at all.

But if you still want to push anyway, you can, and the second part of the error message explains some options you can do.

Normally the repository sits on a server in a bare form, without a working tree. You use repositories with a work tree only in your development environment, and you normally don't push to these repositories.

I'm wondering if you come from Bazaar or similar. In Bazaar it's normal to push and pull between local clones and branches. And unlike Git, it updates the work tree when pushing to a local clone. I knew Bazaar before Git and I was quite confused at first by this (and other things).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top