昨天,我发布了一个关于如何从我的一台机器克隆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/config:

$ cat .git/config 
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[receive]
    denyCurrentBranch = updateInstead

如果核心。Bare为false,你可以设置为true:

$ git config core.bare true

然后在本地推送到远程:

git push remote_repo   // suppose the destination repo is remote_repo

它会成功,在remote_repo你可以检查git版本。

$ git log -1
commit 0623b1b900ef7331b9184722a5381bbdd2d935ba
Author: aircraft < aircraft_xxx@126.com>
Date:   Thu May 17 21:54:37 2018 +0800

现在你不能在你的“工作区”中使用git了:

$ git status
fatal: This operation must be run in a work tree

你应该裸露。裸背为假。

$ git config core.bare false

其他回答

只需几个设置步骤,您就可以轻松地使用一行程序部署更改到您的网站

git push production

这很简单,你不需要登录到远程服务器并进行拉取或其他操作。请注意,如果您不将生产检出用作工作分支,这将是最好的工作方式!(OP的工作环境略有不同,我认为@Robert Gould的解决方案很好地解决了这个问题。此解决方案更适合部署到远程服务器。)

首先,你需要在你的服务器上的某个地方建立一个裸库,在你的webroot之外。

mkdir mywebsite.git
cd mywebsite.git
git init --bare

然后创建文件hooks/post-receive:

#!/bin/sh
GIT_WORK_TREE=/path/to/webroot/of/mywebsite git checkout -f

并使文件可执行:

chmod +x hooks/post-receive

在本地机器上,

git remote add production git@myserver.com:mywebsite.git
git push production +master:refs/heads/master

都准备好了!现在,将来您可以使用git push production来部署您的更改!

这个解决方案的功劳归http://sebduggan.com/blog/deploy-your-website-changes-using-git/。在那里可以找到关于正在发生的事情的更详细的解释。

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

git push <remote> master:origin/master

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

让我加上我的50美分,因为投票最多的答案https://stackoverflow.com/a/3251126/3455918建议将远程repw转换为裸库,如果这不是我想要的呢?

最后,我必须在远程机器上有相同的代码,但不只是.git边缘的某个地方的一团字节。

第二次投票(在撰写本文时)的解决方案https://stackoverflow.com/a/2933656/3455918可以完成这项工作,但在测试之后,我最终不得不不断地在远程机器上的分支之间切换,以“释放”我想从本地机器推送到的分支。

这招对我很管用:

到目前为止对我有效的另一个解决方案不是我的,功劳是给了一个对第一个解决方案发表评论的用户@kxr。

在远程机器上,您必须在repo目录中执行此命令。

git配置receive.denyCurrentBranch updateInstead

在此之后,您就完成了!

显然,这种解决方案可能有一些缺点,但对于将本地机器代码同步到远程回购的简单任务来说,它可能已经足够好了。

如果有人在评论中解释为什么在github上创建一个全新的repo,将你的本地文件夹链接到它,并开始做git push origin master而没有错误,我会很感激。

但是尝试在远程服务器上进行同样的回购会产生错误:

! [远程拒绝]master -> master(分支当前签出)

您应该只推送到一个裸露的存储库。裸存储库是指没有检出分支的存储库。如果要cd到一个裸存储库目录,则只能看到.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