3

I have created a git repository using "git init". Everything works fine but when I execute "git push" from my host it does not push file but gives following errors.

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 root@10.111.44.77:/var/cache/git/h.git
 ! [remote rejected] master -> master (branch is currently checked out)

When I clone this old.git repository to new.git repository using git clone --bare old.git new.git and push file to new.git its work fine.

But when I create a new bare repository with git init --bare then it does not allow "git add" command. It gives following error:

fatal: This operation must be run in a work tree

With git init --bare it does not create a .git folder which it creates with git init.

Any help is appreciated.

4

1 回答 1

4

When you create a git repository with the --bare option, you are creating a repository that does not expect a working tree, just the repository structure.

This means that there is expected not to be an index to add (directly) to. By specifying this, you are setting up a repository that is expected to be pulled and pushed but not added to with the git add command.

When you do git init --bare in your folder, it is what you would expect to see in a .git folder in a project.

branches
config
description
HEAD
hooks
info
objects
refs

This is why you are having the problems you are seeing.

Spend some time in the man pages for git. It is very helpful. Also, they have a wonderful web page with documentation that shows how to manage bare repositories. http://git-scm.org

After reading your comments in your question, I see what you are trying to accomplish.

Edit: Yes, on your server you would want to do the bare repository. You would only push and pull to and from there.

You would then have a "normal" repository where you do your work, potentially on more than one machine. You would do your git add commands and your git pull and git push on there. You will need to add your remote to whatever location your "server" is at. This is not limited to a real server, it can simply be another directory; it could be on a network drive, for example, or a USB thumb-drive.

Git is very flexible, and a good reading of the man pages and documentation is very helpful.

于 2013-08-09T10:26:49.543 回答