W3docs

How to Clone a Single Branch in Git

Sometimes it is necessary for developers to clone just a single git branch. This short tutorial will help you to clone a single and specific branch in Git.

Sometimes it is necessary to clone a single and specific branch. Let’s take a look at different approaches and take the steps to get the job done!

Cloning a Single Branch Using git remote add

Creating a new repository

The first step should be creating a new repository with git init:

git init

git init

Adding the remote and fetching the branch

The second step is to add a remote named origin for the given repository, which will fetch the given branch from origin:

git remote add

git remote add -t <branch-name> -f origin <remote-repo-url>

Checkout to the given branch

The third step is to switch to that branch with the git switch command for creating a corresponding local branch:

How to Clone a Single Branch in Git

git switch <branch-name>

Cloning a Single Branch Using git clone

The classic git clone command with the <kbd class="highlighted">--single-branch</kbd> option will clone only the master branch by default. If you want to clone another branch, you should add the <kbd class="highlighted">--branch</kbd> flag with the name of the desired branch.

git clone a single branch

git clone --single-branch --branch <branch-name> --depth 1 <remote-repo-url>