昨天,我发布了一个关于如何从我的一台机器克隆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 push origin master:foo 并在远程合并它(通过git或pull-request) Git合并foo 强制执行(不推荐,除非你故意通过rebase改变提交): Git push origin master -f 如果仍然被拒绝,禁用远程存储库上的denyCurrentBranch: git config receive.denyCurrentBranch ignore

其他回答

例如,一旦创建了空(裸)存储库,就需要更改远程服务器上的配置文件

root@development:/home/git/repository/my-project# cat config 

你会看到

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

你将使这裸露为假为真,我删除了logallrefupdates = true(不确定它的使用!)

to

[core]
repositoryformatversion = 0
filemode = true
bare = true

您可以测试以下内容

$ git remote show origin
* remote origin
Fetch URL: my-portal@development:/home/XYZ/repository/XYZ
Push  URL: my-portal@development:/home/XYZ/repository/XYZ
HEAD branch: (unknown)

这个HEAD分支:(未知)将显示如果你不能PUSH。因此,如果HEAD分支是未知的,你应该将bare更改为true,在推送成功后,你可以重用

git remote show origin

你们会看到

 HEAD branch: master

您应该只推送到一个裸露的存储库。裸存储库是指没有检出分支的存储库。如果要cd到一个裸存储库目录,则只能看到.git目录的内容。

检查目标项目中的.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 <remote> master:origin/master

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

旧版本的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中的工作目录不同步。您使用的任何正常工作流都应该工作。