37

Is there a way to fork from a specific branch on GitHub? … For example, moodle has many branches (1.9, 2.0 … and so on). Can a clone be performed of just branch 1.9 and not the master branch always? Is it possible to clone a specific branch onto my PC?

2
  • it's not clear if you need to clone or fork a branch Commented Feb 25, 2019 at 18:10
  • You're asking two different questions: 1. Forking on github which is github->github 2. Cloning to your PC which is github->PC. For 2. you can do whatever you want, just check git help clone. Maybe trim down your question and focus on 1.? Commented Dec 5, 2020 at 4:48

9 Answers 9

29

I don’t know a native way yet, but you can do it following this recipe:

  1. Fork the repository in question (called ‘upstream’) on the GitHub website to your workspace there.
  2. Run the GitHub desktop application and clone the repository onto your PC.
  3. Use the GitHub desktop application to open a shell in the repository. (The git commands are not available from the default PowerShell unless you configure that manually.)
  4. Set the source repository as upstream:

    git remote add upstream https://github.com/{user}/{source-repo}.git 
  5. Fetch the full upstream repository. (Right now, you only have a copy of its master branch.)

    git fetch upstream 
  6. Make your file system copy the branch you want and give it any name:

    git checkout upstream/{branch-in-question} git checkout -b temporary 
  7. Publish your repo using the GitHub desktop application.

  8. On the GitHub website, open your repository and click ‘settings’.
  9. Change the “Default branch” to ‘temporary’. (Just change the drop-down menu, you don’t need to click the “Rename” button.)
  10. Go back to your repository, go to the ‘branches’ tab, now you can delete the “master” branch.
  11. Delete the master branch on your shell and make a new master branch:

    git branch -d master git branch master git checkout master git -d temporary 
  12. Once more, publish your repo using the GitHub desktop application.

  13. On the GitHub website, open your repository and click ‘settings’.
  14. Change the “Default branch” back to the (new) ‘master’ branch.
  15. Go back to your repository, go to the ‘branches’ tab, now you can delete the “temporary” branch.

This should be what you were looking for. Perhaps GitHub will provide a more convenient way to do this in future (e.g., clicking “Fork” from a project’s branch results in exactly this behaviour).

Sign up to request clarification or add additional context in comments.

4 Comments

Amazing mountain of steps Batman! Is there an update/more concise method two years later (2015) ?
I didn’t do it again …
I have a forked github repository. After I forked it, the original repo owner created a new branch. I couldn't figure out how to copy that branch over to my fork. These steps worked. Thanks.
5 years later, still no easier way?
11

NO COMMAND LINE NEEDED.

Just create a new branch in your forked repository in GitHub (1), and GitHub will ask you if you want this new branch to be a clone/mirror from any branch in the upstream repository (2). You can give any name to the new branch.

GitHub new branch dialog, with upstream repository branches option

3 Comments

The best solution for me
Then: with VScode/GitLens, open Repositories -> YourRepo -> branches; select and switch-to the new branch. Easy!
Oops: that should have been: open Repositories -> YourRepo -> Remotes; select and switch-to the new branch. Easy! (then it shows up in 'branches')
7

Cloning means that you create a copy of the whole repository in your account including all branches and tags. However you are free to switch and track branches however you like.

7 Comments

... and even to remove branches later.
If the whole repository is cloned,then how come when a 'git branch' command is performed,it just shows the master branch..and not all the branches shown on github?
You can do git branch -d <name> to remove the others locally.
@jan try git branch -a (better late than never)
"Cloning" means many different things, look at git help clone and you will see native git offers many more options than github forking - including --single-branch
|
1

Yes, you can clone the single branch. For example, you have a branch named release1.0. If you would like to clone this branch into your pc then use the following line of code:

$ git clone [email protected]:git_username/git_repository_example -b release1.0 --single-branch 

3 Comments

That's for cloning, not forking.
Questioner has asked this question....Can a clone be performed of just branch 1.9 and not the master branch always? Is it possible to clone a specific branch onto my PC?.. Read the full question before you comment please. Thank you.
Sorry. You're right. Though it would be nice to change the title of the question. I tried your solution and then realized it wouldn't help with my problem.
1

For those who don't like working with command-line. Here is a simple guide using the desktop client for GitHub:

  1. Click the fork button of the repo on GitHub.com: step1

  2. Make sure you have the desktop client installed

  3. Click this button: step2

  4. Clone the repo

step3

  1. In the desktop client, select the desired branch

step4

  1. Select the branch you'd like to work on and you're done

step5

3 Comments

Nice illustrated guide. +1
I believe this still leaves a ton of spurious branches in your fork on the server side. It's not clear why the question is about but git clone --single-branch is not really an issue
@MarcH Yes, if a branch created with this method is not needed anymore it needs to be deleted manually afterwards.
1

I'm posting here the method I've used. Like the OP I wanted to only copy/fork one branch. But couldn't find an easy way.

  • in your repo create a new branch. It doesn't need to have the same name as the branch you want to fork
  • once created, verify that it is the selected branch, and click "Compare"
  • reverse the order of comparison (I have a userscript for that, see my profile if it's something you want to test).
  • the "base" repository must be yours, with the branch you've created
  • the "head" repository is the original, and the branch is the branch you want to fork
  • hit "create pull request" and continue until the PR is applied

That's it. You have the branch forked.

Comments

0

I'm using bitbucket but I'm sure this would work for GitHub as well.

  1. Create a new repository
  2. Checkout the branch using GitExtensions
  3. Click Push to open the Push dialog
  4. Set the destination URL to the new repository
  5. Set the destination branch to "master"
  6. Push

Your new repository will have the full history of the one branch only (not all branches like forking will have).

GitExtensions Push Dialog

Comments

0

A fast, alternative approach is to create your own new repo.

Go to https://github.com/new and make a new repo. Do not initialize with README.

Scroll down to get your git remote

enter image description here

Then:

git remote rm origin git config master.remote origin git config master.merge refs/heads/master // Run code from above image git push --set-upstream origin yourbranchname 

You will have a new repo with the original repo's code and a branch that can be made into a pull request.

Comments

0

SOLUTION:

For remote repository on GitHub and local repository

After fork all branches to your GitHub repository, you can delete Redundant branches in your GitHub repository.

And then you can only clone the branches you need to local.

Step One

Step Two

Only For local repository

git clone -b <branch name> --single-branch <repository> 

If you want to further save your disk space, you can clone remote repository without history:

git clone -b <branch name> --depth 1 <repository> 

notice: --depth implies --single-branch unless --no-single-branch is given.

https://git-scm.com/docs/git-clone

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.