How Do I Clone a Specific Git Branch?

nilan avtar

Written by:

Nilanchala,  2 min read,  updated on September 17, 2023

The git clone is a command-line utility that is used to make a local copy of a remote repository. It accesses the repository through a remote URL. Usually, the original repository is located on a remote server, often from a Git service like GitHub, Bitbucket, or GitLab.

To clone a repository
git clone <remote_repo>

The above command fetches all the branches and checking out one (default master). That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your front page, you may end up cloning 5GB of data.

To clone a specific branch from repository

With Git 1.7.10 and later, add –single-branch to prevent fetching of all branches. Example:

git clone -b feature-001-branch --single-branch <remote_repo>