无法将文件推送到git存储库

时间:2013-08-09 10:00:29

标签: git

我使用“git init”创建了一个git存储库。一切正常但是当我从我的主机执行“git push”时它不会推送文件但会出现以下错误。

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)

当我使用git clone --bare old.git new.git将此old.git存储库克隆到new.git存储库并将文件推送到new.git时,其工作正常。

但是当我使用git init --bare创建一个新的裸存储库时,它不允许“git add”命令。它给出了以下错误:

fatal: This operation must be run in a work tree

使用git init --bare时,它不会创建使用git init创建的.git文件夹。

感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

使用--bare选项创建git存储库时,您创建的存储库不需要工作树,只需要存储库结构。

这意味着预计不会有(直接)添加的索引。通过指定此项,您将设置一个存储库,该存储库应该被拉出并推送但不会添加到git add命令中。

当您在文件夹中执行git init --bare时, 您希望在项目的.git文件夹中看到的内容。

branches
config
description
HEAD
hooks
info
objects
refs

这就是你遇到问题的原因。

花点时间在git的手册页上。这非常有帮助。此外,他们还有一个精彩的网页,其中包含显示如何管理裸存储库的文档。 http://git-scm.org

在您的问题中阅读您的评论后,我会看到您要完成的任务。

编辑 是的,在您的服务器上,您需要执行bare存储库。你只能在那里推拉。

然后,您将拥有一个“正常”存储库,您可以在其中执行您的工作,可能在多台计算机上。您可以执行git add命令,然后使用git pull和git push。您需要将遥控器添加到“服务器”所在的任何位置。这不仅限于真正的服务器,它可以只是另一个目录;例如,它可能位于网络驱动器上,或者是USB拇指驱动器。

Git非常灵活,阅读手册页和文档非常有用。