Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one clone a git repository with only a non-master branch?

Tags:

git

In cases where you don't want to download unneeded files.

like image 767
Trey Piepmeier Avatar asked Aug 18 '09 14:08

Trey Piepmeier


2 Answers

git clone always clones the complete repository unless you specify the --depth <n> parameter which limits the repository to the latest n revisions (a so-called “shallow clone”).

However, you can create a local repository and use git fetch to only fetch parts of the remote repository.

cd /path/foo
git init
git remote add origin <some url>
git fetch origin <some branch>

This will duplicate most of what git clone does but restrict it to the branch(es) you specify on the command line. (I’m not sure about further details such as tracking branches and tags and the like.)

like image 145
Bombe Avatar answered Oct 15 '22 01:10

Bombe


Your question title and summary are kind of asking two different questions because, as Bombe noted, a clone always gets all content unless use use the --depth option.

I'm not sure what you really want but another option is to clone with the --no-checkout (or -n) flag. By default, git will checkout the default branch for the repository (which is determined by the HEAD ref in the remote repository -- it's not always master). If you use the -n flag git will not checkout a branch for you so you can just checkout what you want:

git clone -n <some url> foo
cd foo
git checkout <some branch>
like image 23
Pat Notz Avatar answered Oct 15 '22 00:10

Pat Notz