如何在裸存储库之间同步git分支?

时间:2016-09-15 16:34:04

标签: git

我有一个裸存储库,其origin位于远程计算机上。

我想将其分支机构下载到本地裸仓库中。即我希望在分支列表中使用git branch -vva命令查看它们,如下所示:

* master                0bc84f0 [origin/master] something...
  remotes/origin/HEAD   -> origin/master
  remotes/origin/master 0bc84f0 something...

在非裸存储库的情况下,git pull --all也同步了分支(它使远程分支在本地存储库中可见),但是在裸存储库的情况下,拉不可能。

如何在这种情况下同步远程分支?

注意:git --fetch不起作用,远程分支在它之后仍然不可见:

$ git remote -v
origin  git://host/project.git (fetch)
origin  git://host/project.git (push)
$ git branch -vva
* master 4085a31 ...something
$ git fetch
From git://host/project.git
 * branch            HEAD       -> FETCH_HEAD
$ git branch -vva
* master 4085a31 ...something

其他信息:我的config如下:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = git://host/project.git

我的config在新克隆(也是裸露)的回购中:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = true
[remote "origin"]
        url = git://host/project.git

5 个答案:

答案 0 :(得分:3)

解决方案#1:

您需要将您的仓库转换为镜像仓库:

git remote add --mirror=fetch origin <url>
git fetch

镜像存储将在获取后与源引用同步保留到本地引用。它的缺点是,镜像应该是远程仓库的精确副本,可能不是你的目标。

解决方案#2:

(使用评论从@peterh扩展):

在git配置中(如果是裸仓库,只需repo/config),

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*

应该存在。如果它没有,它可能是一个非平凡的git功能,或git bug。你可以手动修复它。之后,git fetch将起作用。

答案 1 :(得分:3)

git fetch应该已经足够了。

只是为了进行测试,只需尝试将您的裸仓库再次克隆到新的本地仓库中。如果远程跟踪分支在那里,请参阅。

git clone git://host/project.git newproject
cd newproject
git branch -avv
  

我做了一个新的克隆,我仍然看不到任何远程分支,尽管原点指向正确的URL。
  情况仍然相同,你可以在我的问题的最后看到(正确的原始网址,但仍然没有远程分支)

这意味着那些远程分支机构首先不在远程仓库中 克隆的default fetch refspec

fetch = +refs/heads/*:refs/remotes/origin/*

如果远程仓库有任何其他分支,它们也会被克隆。

如果配置没有显示任何fetch键(如git config -l所示),请尝试添加一个:

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

用以下方法检查:

git config --get remote.origin.fetch

点击“git fetch doesn't fetch all branches”了解更多信息。

真正的问题就变成了:OP的配置中只有git clone个克隆只有master并且不包含fetch refspec?

也许是Git environment variable is set
例如,GIT_TEMPLATE_DIR(包括默认配置)

只需添加fetch refspec就可以而不是来解决实际问题 要解决实际问题,需要更多信息(Git版本,操作系统版本......)

答案 2 :(得分:2)

在裸仓库中添加以下行中的配置:

fetch = +refs/*:refs/*

您的配置将如下所示:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = git://host/project.git
    fetch = +refs/*:refs/*

然后在你的回购中

git --bare  fetch

将下载新分支

答案 3 :(得分:1)

git pull是运行git fetch以同步分支的简写,然后git merge将本地副本与远程版本合并。

从您的裸仓库中,您需要做的就是git fetch来同步您当地的分支机构。

有关详细信息,请参阅此处:https://git-scm.com/docs/git-fetch

答案 4 :(得分:1)

一个人必须从:

开始
git clone --mirror git@host/project.git

这会创建一个裸仓库并准备好接收更新 然后cd进入你的project.git并更新你当地的裸仓:

git --bare  fetch

刚试过它,就像魅力一样