Forking only a specific branch from Github repository
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.
First, check whether you have
Naruto
's remote already added usinggit remote -v
. If you see something along the lines ofhttps://github.com/Naruto/O (fetch)
andhttps://github.com/Naruto/O (push)
, you already have the remote added. Skip to step 3.In this step, we'll add the remote of
Naruto
's fork ofO
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 useKyuubi
. Use this command:git remote add Kyuubi https://github.com/Naruto/O
Now, you need to fetch the changes from
Naruto
's repository. Use this command:git fetch Kyuubi
In this step we'll create our own branch called
myB4
fromNaruto
'sB4
. Use this command: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
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
.
xan
Updated on June 11, 2022Comments
-
xan almost 2 years
Suppose there's an official repo maintained called
O
with branchesB1
,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 about 11 yearsIts 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 about 11 yearsWhat exactly is the URL you provided when adding the remote?
-
keelerm about 11 yearsThat 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 about 11 yearsI 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 branchgs
. Please help me! -
keelerm about 11 yearsSee this SO answer which should address the issue you're having. stackoverflow.com/questions/945654/…
-
xan about 11 yearsWas going to it only. I've followed all the steps. The terminal also shows
Everything up-to-date
when Ipush
it to my Github repo. But there's no such branch formed. -
xan about 11 yearsSo 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 about 11 yearsFind the remote name for your Github repo. It's probably
origin
, but you can double-check withgit remote -v
. Then rungit push origin B5
. -
Francois over 2 yearsIf you want to fetch only one branch:
git fetch someuser branchname