简体   繁体   English

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

[英]How can I synchronize git branches between bare repositories?

I have a bare repository, whose origin is on a remote machine. 我有一个裸存储库,它的origin是在远程机器上。

I would like to download its branches into the local bare repo. 我想将其分支下载到本地裸仓库中。 Ie I want to see them with a git branch -vva command in the branch list, like so: 即我想在分支列表中使用git branch -vva命令查看它们,如下所示:

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

In the case of non-bare repos, a git pull --all synchronized also the branches (it made the remote branches visible in the local repository), but in the case of a bare repo, pull is impossible. 在非裸存储库的情况下, git pull --all也同步分支(它使远程分支在本地存储库中可见),但是在裸存储库的情况下,拉不可能。

How can I sync the remote branches in this scenario? 如何在此方案中同步远程分支?


Note: git --fetch doesn't work, the remote branches are still invisible after it: 注意: 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

Additional info: my config is the following: 附加信息:我的config如下:

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

My config in a newly cloned (also bare) repo: 我的config在新克隆(也裸露)回购:

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

Solution #1: 解决方案#1:

You need to convert your repo to a mirror repo: 您需要将您的仓库转换为镜像仓库:

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

A mirror repo will keep to local references in sync with the origin references after a fetch. 在获取后,镜像存储将保持与本地引用同步的原始引用。 It has the disadvantage, that a mirror should be an exact copy of the remote repo, what may be not your goal. 它的缺点是,镜像应该是远程仓库的精确副本,可能不是您的目标。


Solution #2: 解决方案#2:

(Extension from @peterh using the comments): (使用评论从@peterh扩展):

In the git config (simply repo/config in the case of a bare repo), a 在git配置中(在裸仓库的情况下只需repo/config ),a

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

should exist. 应该存在。 If it doesn't, it may be a non-trivial git feature, or a git bug. 如果没有,它可能是一个非平凡的git功能,或git bug。 You can fix it by hand. 你可以手动修复它。 After that, git fetch will work. 之后, git fetch将起作用。

A git fetch should have been enough. git fetch应该已经足够了。

Just for testing, simply try and clone again your bare repo into a new local repo. 只是为了测试,只需尝试将您的裸仓库再次克隆到新的本地仓库中。 See then if the remote tracking branches are there. 如果远程跟踪分支在那里,请参阅。

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

I've did a new clone, and I still can't see any remote branches, despite that the origin points to the correct url. 我做了一个新的克隆,我仍然看不到任何远程分支,尽管原点指向正确的URL。
The situation is still the same, as you can see at the end of my question (correct origin URL, but still no remote branches) 情况仍然相同,你可以在我的问题的最后看到(正确的原始URL,但仍然没有远程分支)

That means those remote branches are not in the remote repo in the first place. 这意味着那些远程分支机构首先不在远程仓库中。
The default fetch refspec for a clone is 克隆的默认提取refspec

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

If the remote repo had any other branches, they would have been cloned as well. 如果远程仓库有任何其他分支,它们也将被克隆。

If the config does not show any fetch key (as seen with git config -l ), try and add one: 如果配置没有显示任何fetch密钥(如git config -l ),请尝试添加一个:

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

Check that with: 用以下方法检查:

git config --get remote.origin.fetch

See more at " git fetch doesn't fetch all branches ". 在“ git fetch不能获取所有分支 ”中查看更多信息。


The true question then becomes: what in the OP's config makes a git clone clones only master and include no fetch refspec? 那么真正的问题就是:在OP的配置中, git clone克隆只有master并且不包含fetch refspec?

Maybe a Git environment variable is set ? 也许设置了Git环境变量
For instance, GIT_TEMPLATE_DIR (which would include a default config) 例如, GIT_TEMPLATE_DIR (包括默认配置)

Simply adding the fetch refspec does not solve the actual issue. 简单地增加抓取的Refspec 不能解决实际问题。
For solving the actual issue, more information is needed (Git version, OS version, ...) 要解决实际问题,需要更多信息(Git版本,操作系统版本......)

add to your config in the bare repo the following line : 在以下行的裸仓库中添加到您的配置:

fetch = +refs/*:refs/*

your config would then look like: 您的配置将如下所示:

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

and then in your repo do 然后在你的回购做

git --bare  fetch

and new branches will be downloaded 将下载新的分支机构

git pull is shorthand for running git fetch to synchronize the branch, then git merge to merge the local copy with the remote version. git pull是运行git fetch以同步分支的简写,然后是git merge以将本地副本与远程版本合并。

From your bare repo, all you need to do is git fetch to sychronize your local branch refs. 从你的裸仓库,你需要做的就是git fetch来同步你的本地分支引用。

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

One would have to start with: 一个人必须从:

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

this creates a bare repo and is ready to receive updates then cd into your project.git and to update your local bare repo: 这会创建一个裸仓库,并准备好接收更新,然后cd到您的project.git并更新您当地的裸仓库:

git --bare  fetch

Just tested it and works like a charm 只是测试它,就像一个魅力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM