昨天,我发布了一个关于如何从我的一台机器克隆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版本)。这是一个可能的原因吗?


当前回答

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

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

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

其他回答

你有三种选择

又拉又推: git拉;git推 推入不同分支: Git push origin master:foo 并在远程合并它(通过git或pull-request) Git合并foo 强制执行(不推荐,除非你故意通过rebase改变提交): Git push origin master -f 如果仍然被拒绝,禁用远程存储库上的denyCurrentBranch: git config receive.denyCurrentBranch ignore

错误消息描述发生了什么。更现代的Git版本在分支签出时拒绝通过推送更新该分支。

在两个非裸存储库之间工作的最简单方法是

总是通过pull(或fetch和merge)来更新存储库,如果必须的话, 通过推入到一个单独的分支(导入分支),然后将该分支合并到远程机器上的主分支。

这个限制的原因是推送操作只能在远程Git存储库上操作,它不能访问索引和工作树。因此,如果允许的话,对签出分支的推操作将使HEAD与远程存储库上的索引和工作树不一致。

这将非常容易意外地提交一个更改来撤销所有已推送的更改,也使得很难区分尚未提交的任何局部更改和新HEAD、索引和工作树之间的差异,这些差异是由推送移动HEAD引起的。

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

git push <remote> master:origin/master

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

我相信大多数看到这个问题的人会止步于前两个重要的答案,但我仍然想提供我的解决方案。

当遇到描述的错误时,我有一个Eclipse + EGit web项目设置。帮助我的是使用GitHub应用程序,它似乎神奇地解决了这个问题。虽然EGit总是拒绝推送,但GitHub桌面应用程序只会耸耸肩,推送我的更改。也许它能更优雅地处理多次登录的情况。

我也有同样的问题。对我来说,我使用Git push将代码移动到服务器。我从不改变服务器端的代码,所以这是安全的。

在存储库中,您正在按下输入:

git config receive.denyCurrentBranch ignore

这将允许您在存储库是工作副本时更改存储库。

运行Git推送后,转到远程机器并键入以下内容:

git checkout -f

这将使您所推动的更改反映在远程计算机的工作副本中。

请注意,如果在您要推入的工作副本中进行更改,这并不总是安全的。