Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: Clone only specific branches from GitHub

Tags:

Is there a potential way to clone not complete repository from GitHub, but just selected branches? I've found single branch clone is possible with command:

git clone git@github/path/to/repository.git --branch my_branch_1 --single-branch

So would like to achieve something like this:

git clone git@github/path/to/repository.git --branch my_branch_1 --branch my_branch_2 --single-branch ??

It means only two of them. The issue is, that such repo is quite huge in the master branch and not needed for developers. They just need branches my_branch_1 and my_branch_2. From such branches developers should make their dev branch and later pull request on GitHub to the master.

Maybe it's possible via git remote add or something like this. But I'm not so much familiar probably with the concept of Git internally.

like image 288
David Flegl Avatar asked Oct 18 '19 07:10

David Flegl


People also ask

How do I clone a specific branch?

In order to clone a specific branch, you have to execute “git branch” with the “-b” and specify the branch you want to clone. $ git clone -b dev https://github.com/username/project.git Cloning into 'project'...

Can I clone only one branch?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.

How do you get specific branch from remote?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.


1 Answers

There doesn't seem to a way to clone multiple branches, but you can clone just one, then fetch the remainder like so:

git clone git@github/path/to/repository.git --branch my_branch_1 --single-branch
git fetch origin my_branch_2:my_branch_2 my_branch_3:my_branch_3
like image 78
Ken Y-N Avatar answered Sep 29 '22 01:09

Ken Y-N