Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone only one branch [duplicate]

Tags:

git

github

I would like to know how I could clone only one branch instead of cloning the whole Git repository.

like image 393
max_ Avatar asked Jan 26 '11 23:01

max_


People also ask

How do I clone only a specific 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 I clone a specific branch in Azure DevOps?

From your web browser, open the team project for your Azure DevOps organization, and then choose Repos > Files to open the Files view. In the Files view, choose Clone to launch the Clone Repository popup. Copy the clone URL from the Clone Repository popup.

Does git clone Get all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.


2 Answers

From the announcement Git 1.7.10 (April 2012):

  • git clone learned --single-branch option to limit cloning to a single branch (surprise!); tags that do not point into the history of the branch are not fetched.

Git actually allows you to clone only one branch, for example:

git clone -b mybranch --single-branch git://sub.domain.com/repo.git 

Note: Also you can add another single branch or "undo" this action.

like image 146
shakaran Avatar answered Oct 02 '22 21:10

shakaran


You could create a new repo with

git init  

and then use

git fetch url-to-repo branchname:refs/remotes/origin/branchname 

to fetch just that one branch into a local remote-tracking branch.

like image 28
Lily Ballard Avatar answered Oct 02 '22 22:10

Lily Ballard