简体   繁体   English

如何从存储库克隆并使用git带来所有远程分支?

[英]How to clone from a repository and bring all remote branches using git?

I Have repository 'A', I cloned from it to Repository 'B' using git clone http://example/A.git . 我有存储库“ A”,我使用git clone http://example/A.git从它克隆到存储库“ B” So now, I have one local branch "master" and all other branches as remote branches in 'B'. 因此,现在,我有一个本地分支“ master”,而所有其他分支都是“ B”中的远程分支。

Next Step, I want to clone from 'B' to a new Repository 'C' using git clone --bare , and bring all remote branches from 'B' and make them as local at 'C'. 下一步,我想使用git clone --bare从'B'克隆到新的存储库'C',并从'B'引入所有远程分支并将它们作为'C'的本地分支。

Image of My Workflow 我的工作流程图片

just clone B to C 只需将B复制到C

and run 并运行

git branch -a

to see all hidden branches 查看所有隐藏的分支

You can see full details in this answer: 您可以在此答案中看到完整的详细信息:

How to clone all remote branches in Git? 如何在Git中克隆所有远程分支?

You can create a mirror clone, but a mirror clone is a bare clone (for good reasons). 您可以创建镜像克隆,但是镜像克隆是裸克隆(有充分的理由)。 That is: 那是:

[host B] git clone --mirror http://example/A.git

produces a mirror clone on B, and you can now do git clone --bare from B to C : 在B上生成镜像克隆,您现在可以执行git clone --bareBC

[host C] git clone --bare <url of B.git>

The mirror clone on B will contain all references that A gave, while the bare clone on C will contain only the branch and tag references from A. That is, if repository A has references refs/heads/master , refs/heads/develop , refs/remotes/origin/master , refs/replace/a23456789a23456789a23456789a23456789 , and refs/tags/v1.0 , the mirror on B has all these same references, while the bare clone on C lacks the refs/remotes/origin/master and refs/replace/... reference. B上的镜像克隆将包含A给出的所有引用,而C上的裸克隆将仅包含来自A的分支和标记引用。也就是说,如果存储库A的引用为refs/heads/masterrefs/heads/developrefs/remotes/origin/masterrefs/replace/a23456789a23456789a23456789a23456789refs/tags/v1.0 ,B上的镜像具有所有这些相同的引用,而C上的裸克隆缺少refs/remotes/origin/masterrefs/replace/...参考。

At any point after each clone, you can create refs/heads/ references within the clone. 在每个克隆之后的任何时候,您都可以在克隆内创建refs/heads/ references。 Hence if you want a non-bare repository on B, you can replace the middle step with: 因此,如果您希望在B上放置一个非裸露的存储库,则可以将中间步骤替换为:

[host B] git clone http://example/A.git

followed by a series of git update-ref (for arbitrary reference names) or git branch (for refs/heads/* reference names) operations. 随后是一系列的git update-ref (对于任意参考名称)或git branch (对于refs/heads/*参考名称)操作。 But note that the B clone at this point has copied neither A's refs/remotes/origin/master nor A's refs/replace/... reference. 但是请注意,此时B克隆没有复制A的refs/remotes/origin/master或A的refs/replace/...引用。 B has: B有:

  • A's refs/heads/master , which B calls refs/remotes/origin/master now A的refs/heads/master ,B现在称为refs/remotes/origin/master
  • A's refs/heads/develop , which B calls refs/remotes/origin/develop now A的refs/heads/develop ,B现在称为refs/remotes/origin/develop
  • A's refs/tags/v1.0 , which B calls refs/tags/v1.0 A的refs/tags/v1.0 ,B称之为refs/tags/v1.0

That is, B has copied A's refs/heads/* (branch) references, but has renamed them to be remote-tracking names instead of branch names. 也就是说,B复制了A的refs/heads/* (分支)引用,但已将其重命名远程跟踪名称而不是分支名称。 B has copied A's tags without any name changes. B复制了A的标签,但未更改任何名称。

As the last step of its clone operation, B ran git checkout master (or maybe git checkout develop if A told it to use develop instead). 作为其克隆操作的最后一步,B跑git checkout master (或者git checkout develop ,如果A告诉它用develop来代替)。 This has created B's only branch name, refs/heads/master (or maybe refs/heads/develop ) and populated B's index and work-tree. 这样就创建了B的唯一分支名称refs/heads/master (或者可能是refs/heads/develop ),并填充了B的索引和工作树。 So if you would like to make B have, as branches, all the branches that A had, it's relatively easy: 因此,如果您想让B拥有A拥有的所有分支作为分支,则相对容易:

for all branches that were in A, but are now remote-tracking names in B:
    create branch in B using the remote-tracking name we created during the clone

which you can express in whatever programming language you prefer. 您可以用自己喜欢的任何编程语言来表达。

There is one annoyance here, which is that the "create branch" step will fail for the one branch that was already created by the git checkout step at the end of git clone . 这里有一个烦人的地方,那就是对于git clone末尾的git checkout步骤已经创建的一个分支,“ create branch”步骤将失败。 To avoid the annoyance, either check for and skip that branch, or use git clone --no-checkout to avoid creating the branch and populating the index and work-tree on B . 为了避免烦恼,请检查并跳过该分支,或者使用git clone --no-checkout避免创建分支并在B上填充索引和工作树。

Summary 摘要

  • If you want a bare clone on B, using --mirror is an option. 如果要在B上进行克隆,则可以使用--mirror You will get all references. 您将获得所有参考。
  • If you want a bare clone on B but not a mirror, use --bare . 如果要在B上裸露克隆而不是镜像,请使用--bare You will get all branch and tag references. 您将获得所有分支和标签引用。
  • If you want a non-bare clone on B, you must work harder (see above). 如果要在B上进行非裸克隆,则必须更加努力(请参见上文)。

Repeat this for C. 对C重复此操作。

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

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