0

我还没有从这里的众多答案和博客文章中拼凑出我想知道的东西,所以我问了这个问题:

如何分叉一个 git repo 以便获得上游的所有分支和标签?

我试过的是:

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

但是当我期望它起作用时,最后一行失败了。我明白了:

$ 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>

有任何想法吗?

编辑:

也许你可以把这个问题想象成“当你点击 fork 按钮时 GitHub 实际上做了什么?”,因为这就是我想要复制的行为。

4

2 回答 2

4

试试镜子:

git clone --mirror

这样,origin在没有分支命名空间的情况下创建了远程(命名)。IOW,镜像中的 refs/heads/master 完全对应(并绑定到)原始存储库中的 refs/heads/master。

现在只需使用此存储库而不是原始上游存储库。如果您想更新镜像,请在其中执行以下操作:

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

警告:如果你推入你的镜像,修改上游分支,上游 repo 和你的镜像将会发散。并且 push (没有-f选项)将失败,以防止这种情况发生。

Mirror 实际上是上游 repo 的精确镜像,作为它的所有者,您可以选择对其进行修改。

于 2012-07-16T09:07:25.797 回答
1

这是我想出的最好的方法来做我想做的事情,这似乎可行,并且与 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

然后调整.git/config以将upstream遥控器更改为不再镜像,因为这肯定不是我现在想要的。

于 2012-07-17T10:20:05.490 回答