13

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?

xan
  • 4,640
  • 13
  • 50
  • 83

3 Answers3

13

You can pull his branch into your local git repo, and then push it up to your GitHub hosted repo.

First, add a remote to this other users's GitHub page

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.

git push origin B4:B4
keelerm
  • 2,873
  • 20
  • 12
  • Its giving a fatal error. The url of the user's repo is something like "user/repo/tree/sg/search". I've added this path only to remote. – xan Mar 26 '13 at 18:10
  • What exactly is the URL you provided when adding the remote? – keelerm Mar 26 '13 at 18:15
  • That isn't the correct path. Go to that user's GitHub page for that repo and copy the URL provided towards the top. It's the same one you use to initially clone the repo. – keelerm Mar 26 '13 at 18:48
  • I tried doing it with that url only. Omit, the other URL I wrote here before. But still I'm getting this error: `fatal: git checkout: updating paths is incompatible with switching branches.` You can view the repo here: `https://github.com/tmuras/moodle`. I want to fork the branch `gs`. Please help me! – xan Mar 26 '13 at 18:54
  • See this SO answer which should address the issue you're having. http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work – keelerm Mar 26 '13 at 19:00
  • Was going to it only. I've followed all the steps. The terminal also shows `Everything up-to-date` when I `push` it to my Github repo. But there's no such branch formed. – xan Mar 26 '13 at 19:06
8

Add that user's repository as a "remote repository" of your working directory:

git remote add someuser https://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:

git checkout -b B5 someuser/B4

That is, create a new branch (-b) called B5, using someuser/B4 as the starting point.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • So far I've done whatever you instructed. While doing `git branch -r` it shows the repo under the remote `/someuser`. Now, how can I push it to my Github repo? – xan Mar 26 '13 at 19:17
  • Find the remote name for your Github repo. It's probably `origin`, but you can double-check with `git remote -v`. Then run `git push origin B5`. – legoscia Mar 27 '13 at 09:36
  • If you want to fetch only one branch: `git fetch someuser branchname` – Donshel Dec 23 '21 at 10:34
5

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.

Rahul Saxena
  • 630
  • 7
  • 10