Reputation: 6151
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?
Upvotes: 37
Views: 36220
Reputation: 616
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.
Upvotes: 10
Reputation: 687
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.
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
Upvotes: 0
Reputation: 11
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.
That's it. You have the branch forked.
Upvotes: 1
Reputation: 14890
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
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.
Upvotes: 0
Reputation: 15973
For those who don't like working with command-line. Here is a simple guide using the desktop client for GitHub:
Make sure you have the desktop client installed
Clone the repo
Upvotes: 1
Reputation: 151
I'm using bitbucket but I'm sure this would work for GitHub as well.
Your new repository will have the full history of the one branch only (not all branches like forking will have).
Upvotes: 0
Reputation: 1370
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
Upvotes: 1
Reputation: 10102
I don’t know a native way yet, but you can do it following this recipe:
git
commands are not available from the default PowerShell unless you configure that manually.)Set the source repository as upstream:
git remote add upstream https://github.com/{user}/{source-repo}.git
Fetch the full upstream repository. (Right now, you only have a copy of its master branch.)
git fetch upstream
Make your file system copy the branch you want and give it any name:
git checkout upstream/{branch-in-question}
git checkout -b temporary
Publish your repo using the GitHub desktop application.
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
Once more, publish your repo using the GitHub desktop application.
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).
Upvotes: 28
Reputation: 16012
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.
Upvotes: 7