无法将文件推送到git仓库。

3
我使用 "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将旧的.git仓库克隆到新的.git仓库并将文件推送到新的.git仓库时,一切正常。

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

fatal: This operation must be run in a work tree

使用 "git init --bare" 命令时,不会像"git init" 命令一样创建 ".git" 文件夹。
感谢您的阅读。

你所描述的是完全正常的,这正是裸仓库的工作方式。不过我不确定你的问题是什么,因为这里没有什么需要“修复”的。 - Mat
@Mat 我的意思是,我如何将我在本地机器上所做的更改推送到服务器上,因为“git status”会显示“比原始/主分支多一个提交”,直到我成功推送它。 - Sumit Rathore
@Mat告诉我推送或拉取文件需要裸仓库,而添加则需要非裸仓库。这意味着对于单个项目,我需要两个仓库? - Sumit Rathore
很抱歉,我不明白你的意思。如果你想要推送或拉取某些东西,当然需要仓库。你不能从一个仓库推送到它自己。如果你不需要在任何地方推送或拉取,单个(非裸)仓库就可以正常工作。 - Mat
@Mat 看,我在服务器上有一个非裸库,我创建了一个文件并提交了它。现在,在我的本地机器上,我克隆了服务器的库并对文件进行了进一步更改。现在,这些更改如何反映到服务器的库中?使用本地机器上的推送会提示需要裸库。所以我的问题是,为了推送这个更改后的文件,我是否需要另一个裸库来补充我在服务器上的非裸库。 - Sumit Rathore
显示剩余4条评论
1个回答

4
当你使用 --bare 选项创建 git 仓库时,你创建的是一个不需要工作树(working tree)的仓库,只有仓库结构。
这意味着预计不会有索引(index)直接添加到其中。通过指定这个选项,你设置了一个仅用于拉取和推送而不使用 git add 命令添加的仓库。
当你在文件夹中执行 git init --bare 命令时,它就是你在项目的 .git 文件夹中所期望看到的内容。
branches
config
description
HEAD
hooks
info
objects
refs

这就是你所遇到的问题的原因。
花些时间在git的man页面上。这很有帮助。此外,他们有一个精彩的网页文档,展示如何管理裸库。http://git-scm.org 根据你在问题中的评论,我了解到你试图实现什么目标。 编辑:是的,在你的服务器上,你需要使用裸库。你只需向那里推送和拉取即可。
然后,你将拥有一个“正常”的存储库,在其中可以在多台机器上进行工作。你会在那里执行你的git add命令以及git pull和git push命令。你需要将你的远程添加到任何位置,你的“服务器”在哪里都可以。这不限于真正的服务器,它可以是另一个目录;例如,它可以是在网络驱动器上或USB闪存驱动器上。
Git非常灵活,阅读man页面和文档非常有帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接