[英]Forking only a specific branch from Github repository
Suppose there's an official repo maintained called O
with branches B1
, B2
& B3
.假设有一个名为
O
的官方仓库维护,其分支B1
、 B2
和B3
。
One user, who has forked it onto his Github account, made another branch for himself called B4
and is publicly available.一位用户将它分叉到他的 Github 帐户,为自己创建了另一个名为
B4
分支,并且是公开可用的。
I've also forked the same official repo but I want to fork that user's B4
branch also without affecting my original copy.我也分叉了相同的官方仓库,但我也想分叉该用户的
B4
分支而不影响我的原始副本。
I cannot fork the whole official repo again as I've made several custom branches for myself.由于我为自己制作了几个自定义分支,因此我无法再次分叉整个官方仓库。
So, how can I fork a particular branch onto my Github repo?那么,如何将特定分支分叉到我的 Github 存储库上?
You can pull his branch into your local git repo, and then push it up to your GitHub hosted repo.您可以将他的分支拉入您本地的 git 存储库,然后将其推送到您的 GitHub 托管存储库。
First, add a remote to this other users's GitHub page首先,在这个其他用户的 GitHub 页面上添加一个遥控器
git remote add other-user http://github.com/otheruser/repo
Then make a local checkout of that branch in your repo.然后在您的回购中对该分支进行本地结帐。
git checkout -b B4 other-user/B4
Finally, push that branch up to your repo hosted on GitHub.最后,将该分支推送到托管在 GitHub 上的存储库。
git push origin B4:B4
Add that user's repository as a "remote repository" of your working directory:将该用户的存储库添加为您工作目录的“远程存储库”:
git remote add someuser git://github.com/someuser/somerepo.git
Once you've done that, you need to fetch the changes from that user's repository.完成后,您需要从该用户的存储库中获取更改。 Later on, you can do that at any time, without affecting anything else in your local repo.
稍后,您可以随时执行此操作,而不会影响本地存储库中的任何其他内容。
git fetch someuser
And branch that user's B4
into your own B5
:并将该用户的
B4
分支到您自己的B5
:
git checkout -b B5 someuser/B4
That is, create a new branch ( -b
) called B5
, using someuser/B4
as the starting point.也就是说,创建一个名为
B5
的新分支 ( -b
),使用someuser/B4
作为起点。
Although the answer provided by @keelerm , is correct, but it may lead to some confusions because of the naming convention followed in that answer.尽管@keelerm 提供的答案是正确的,但由于该答案中遵循的命名约定,它可能会导致一些混淆。
Let's assume the user, whose branch you want to clone has the github username Naruto
.假设您要克隆其分支的用户具有 github 用户名
Naruto
。 So, basically put Naruto
has created a branch B4
from the official repository O
that you want on your system.所以,基本上把
Naruto
从你想要在你的系统上的官方存储库O
创建了一个分支B4
。
First, check whether you have Naruto
's remote already added using git remote -v
.首先,使用
git remote -v
检查您是否已经添加了Naruto
的遥控git remote -v
。 If you see something along the lines of https://github.com/Naruto/O (fetch)
and https://github.com/Naruto/O (push)
, you already have the remote added.如果您看到类似
https://github.com/Naruto/O (fetch)
和https://github.com/Naruto/O (push)
,则您已经添加了遥控器。 Skip to step 3.跳到第 3 步。
In this step, we'll add the remote of Naruto
's fork of O
so that we can fetch all information from it.在这一步中,我们将添加
Naruto
的O
叉的遥控器,以便我们可以从中获取所有信息。 Choose any handy name that you'll use to refer to the remote.选择您将用来指代遥控器的任何方便的名称。 For illustration purposes, I'll use
Kyuubi
.出于说明目的,我将使用
Kyuubi
。 Use this command: git remote add Kyuubi https://github.com/Naruto/O
使用这个命令:
git remote add Kyuubi https://github.com/Naruto/O
Now, you need to fetch the changes from Naruto
's repository.现在,您需要从
Naruto
的存储库中获取更改。 Use this command: git fetch Kyuubi
使用这个命令:
git fetch Kyuubi
In this step we'll create our own branch called myB4
from Naruto
's B4
.在这一步中,我们将从
Naruto
的B4
创建我们自己的分支myB4
。 Use this command: git checkout -b myB4 Naruto/B4
使用这个命令:
git checkout -b myB4 Naruto/B4
If you need this myB4
branch to be reflected right away in your Github too, with the same name, use this command: git push origin myB4:myB4
如果你需要这个
myB4
分支也立即反映在你的 Github 中,使用相同的名称,使用这个命令: git push origin myB4:myB4
That is it.这就对了。 Now you have a branch named
myB4
from Naruto
's forked repository O
and your branch myB4
contains the same information as Naruto
's B4
.现在您有一个来自
Naruto
的分叉存储库O
名为myB4
的分支,并且您的分支myB4
包含与Naruto
的B4
相同的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.