简体   繁体   English

仅从 Github 存储库中分叉特定分支

[英]Forking only a specific branch from Github repository

Suppose there's an official repo maintained called O with branches B1 , B2 & B3 .假设有一个名为O的官方仓库维护,其分支B1B2B3

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

  1. 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 步。

  2. In this step, we'll add the remote of Naruto 's fork of O so that we can fetch all information from it.在这一步中,我们将添加NarutoO叉的遥控器,以便我们可以从中获取所有信息。 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

  3. Now, you need to fetch the changes from Naruto 's repository.现在,您需要从Naruto的存储库中获取更改。 Use this command: git fetch Kyuubi使用这个命令: git fetch Kyuubi

  4. In this step we'll create our own branch called myB4 from Naruto 's B4 .在这一步中,我们将从NarutoB4创建我们自己的分支myB4 Use this command: git checkout -b myB4 Naruto/B4使用这个命令: git checkout -b myB4 Naruto/B4

  5. 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包含与NarutoB4相同的信息。

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

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