简体   繁体   English

完全分叉git repo?

[英]Fully fork a git repo?

I've not quite managed to piece together what I want to know from the multitude of answers on here and blog posts, so I'm asking this question: 从这里的大量答案和博客文章中,我还没有设法将我想知道的内容拼凑起来,所以我问这个问题:

How do I fork a git repo such that I get all the branches & tags of the upstream? 如何分叉git repo,以便获得上游的所有分支和标签?

What I've tried is: 我试过的是:

git init
git remote add origin <ORIGIN_URL> # i.e. my repo
git remote add upstream <UPSTREAM_URL> # i.e. the repo I want to fork
git fetch upstream
git push --all origin

But that last line fails when I would expect it to work. 但是当我希望它能工作时,最后一行失败。 I get this: 我得到这个:

$ git push --all origin
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
fatal: The remote end hung up unexpectedly
error: failed to push some refs to <ORIGIN_URL>

Any ideas? 有任何想法吗?

EDIT: 编辑:

Maybe you can think of this question as "What does GitHub actually do when you click the fork button?", as that's the behaviour I am trying to replicate. 也许您可以将这个问题想成“当您单击fork按钮时GitHub实际上会做什么?”,因为这就是我要复制的行为。

Try a mirror: 试一面镜子:

git clone --mirror

This way, the remote (named origin ) is created without a namespace for branches. 这样,就创建了远程(命名为origin ),而没有分支的名称空间。 IOW, the refs/heads/master in the mirror corresponds exactly (and is bound to) the refs/heads/master in the origin repository. IOW,镜像中的refs / heads / master完全对应(并绑定到)原始存储库中的refs / heads / master。

Now just use this repository instead of original upstream repository. 现在仅使用此存储库,而不是原始的上游存储库。 If you ever want to update the mirror, do something like this in it: 如果您想更新镜像,请在其中执行以下操作:

cd upstream-mirror.git && git --bare fetch upstream

Warning: if you ever push into your mirror, modifying the upstreams branches, the upstream repo and your mirror will diverge. 警告:如果您推入镜像,修改上游分支,上游存储库和镜像将分开。 and push (without -f option) will fail, to prevent that. 和push(不带-f选项)将失败,以防止这种情况。

Mirror is actually an exact mirror of the upstream repo, with you, as its owner, having an option to modify it. 镜像实际上是上游存储库的精确镜像,您(作为所有者)可以选择对其进行修改。

This is what I've come up with as the best way to do what I wanted which seems to work and gets a similar behaviour to what GitHub does: 这是我想出的最好方法,可以做我想做的事情,它似乎可以正常工作,并且具有与GitHub相似的行为:

git clone --mirror <UPSTREAM> myrepo
cd myrepo
git remote rename origin upstream
git remote add origin <MY_ORIGIN>
git push --all
git push --tags

And then tweak the .git/config to change the upstream remote to not mirror anymore as that's certainly not what I want now. 然后调整.git/config以更改upstream远程服务器不再镜像,因为这肯定不是我现在想要的。

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

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