Forking only a specific branch from Github repository

14,595

Solution 1

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

Solution 2

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:

git checkout -b B5 someuser/B4

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

Solution 3

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.

Share:
14,595
xan
Author by

xan

Updated on June 11, 2022

Comments

  • xan
    xan almost 2 years

    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
    xan about 11 years
    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.
  • keelerm
    keelerm about 11 years
    What exactly is the URL you provided when adding the remote?
  • keelerm
    keelerm about 11 years
    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.
  • xan
    xan about 11 years
    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!
  • keelerm
    keelerm about 11 years
    See this SO answer which should address the issue you're having. stackoverflow.com/questions/945654/…
  • xan
    xan about 11 years
    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
    xan about 11 years
    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?
  • legoscia
    legoscia about 11 years
    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.
  • Francois
    Francois over 2 years
    If you want to fetch only one branch: git fetch someuser branchname