昨天,我发布了一个关于如何从我的一台机器克隆Git存储库到另一台机器的问题,我如何从另一台机器“克隆Git”?

现在我可以成功地将Git存储库从源存储库(192.168.1.2)克隆到目标存储库(192.168.1.1)。

但是当我对文件进行编辑,git commit -a -m "test"和git push时,我在目的地(192.168.1.1)上得到了这个错误:

git push                                                
hap@192.168.1.2'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://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'

我正在使用两个不同版本的Git(远程上的1.7版本和本地机器上的1.5版本)。这是一个可能的原因吗?


当前回答

我刚刚在Heroku上部署git存储库时遇到了这个问题。

我不知道为什么Heroku在他们的一方有一个非裸库,但作为一个变通办法,我能够重置远程库,并重新加载。

你不应该使用Heroku的存储库副本作为你唯一的git存储库进行协作,但为了以防万一,我要明确地说:除非你确定你有一个存储库的完整副本安全地存储在Heroku之外的地方,否则不要这样做。重置操作将删除存储库内容。

重置:

如果您还没有安装Heroku工具带(其中包含命令行客户端)。 如果你还没有安装heroku-repo插件。 Heroku插件:安装https://github.com/heroku/heroku-repo.git 执行重置,删除存储库并创建一个新的空存储库 heroku回购:重置 像往常一样按下Heroku遥控器;它会重新加载所有内容。

其他回答

我刚开始学习Git时也犯过同样的错误。其他一些答案显然不适合Git新手!

我要用一些非专业术语来表达我的意思。不管怎样,你有两个存储库,一个是你第一次做的原始的,另一个是你刚刚做的工作。

现在您在工作存储库中,正在使用主分支。但是您碰巧也在原始存储库中“登录”到同一个主分支。现在,由于您是在原始版本中“登录”的,Git担心您可能会搞砸,因为您可能正在使用原始版本并把事情搞砸。因此,您需要返回到原始存储库并执行git检出其他分支,现在您可以毫无问题地进行推送。

使用这个将它推到远程上游分支为我解决了这个问题:

git push <remote> master:origin/master

远程无法访问上游回购,因此这是将最新更改导入远程的好方法

您可以通过在目标服务器上编辑.git/config来绕过这个“限制”。添加以下命令,允许git存储库被推入,即使它被“签出”:

[receive]
denyCurrentBranch = warn

or

[receive]
denyCurrentBranch = false

第一个将允许推送,同时警告可能会弄乱分支,而第二个将只是静静地允许它。

这可以用于将代码“部署”到不用于编辑的服务器上。这不是最好的方法,但对于部署代码来说是一种快速的方法。

以防有人觉得有用。对我来说,这是一个git服务器权限问题。我检查了项目从乞求和推送一个简单的文件,然后我得到“推送拒绝:推送到原点/主被拒绝”

旧版本的Git允许推入当前签出的非裸库分支。

事实证明,这是一件非常令人困惑的事情。所以他们添加了你看到的警告信息,这也是非常令人困惑的。

如果第一个存储库只是充当服务器,那么按照其他答案的建议将其转换为裸存储库,然后就可以完成了。

然而,如果你需要在两个都在使用的回购之间有一个共享分支,你可以通过下面的设置来实现

Repo1 -将充当服务器,也用于开发

Repo2—仅用于开发

按如下方式安装Repo1

创建一个分支来共享工作。

git branch shared_branch

为了安全起见,您还应该创建一个$(REPO)。Git /hooks/update拒绝对shared_branch以外的任何内容的任何更改,因为你不想让别人乱动你的私有分支。

repo1/.git/hooks  (GIT_DIR!)$ cat update
#!/bin/sh
refname="$1"
oldrev="$2"
newrev="$3"

if [ "${refname}" != "refs/heads/shared_branch" ]
then
   echo "You can only push changes to shared_branch, you cannot push to ${refname}"
   exit 1
fi

现在在repo1中创建一个本地分支,您将在其中执行实际工作。

git checkout -b my_work --track shared_branch
Branch my_work set up to track local branch shared_branch.
Switched to a new branch 'my_work'

(可能需要git配置——global push.default upstream,以便git push工作)

现在您可以使用

git clone path/to/repo1 repo2 
git checkout shared_branch 

此时,您已经设置了repo1和repo2,以便在本地分支上工作,从repo1中的shared_branch中进行推和拉操作,而无需担心错误消息或repo1中的工作目录不同步。您使用的任何正常工作流都应该工作。