Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forking only a specific branch from Github repository

Tags:

git

github

fork

Suppose there's an official repo maintained called O with branches B1, B2 & B3.

One user, who has forked it onto his Github account, made another branch for himself called B4 and is publicly available.

I've also forked the same official repo but I want to fork that user's B4 branch also without affecting my original copy.

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?

like image 888
xan Avatar asked Mar 26 '13 17:03

xan


1 Answers

Although the answer provided by @keelerm , is correct, but it may lead to some confusions because of the naming convention followed in that answer.

Let's assume the user, whose branch you want to clone has the github username Naruto. So, basically put Naruto has created a branch B4 from the official repository O that you want on your system.

  1. First, check whether you have Naruto's remote already added using 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. Skip to step 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. Choose any handy name that you'll use to refer to the remote. For illustration purposes, I'll use Kyuubi. Use this command: git remote add Kyuubi https://github.com/Naruto/O

  3. Now, you need to fetch the changes from Naruto's repository. Use this command: git fetch Kyuubi

  4. In this step we'll create our own branch called myB4 from Naruto's B4. Use this command: 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

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.

like image 159
Rahul Saxena Avatar answered Oct 13 '22 00:10

Rahul Saxena