Introduction
By default, cloning a Git repository creates a copy of the entire remote repository on the local machine. However, in some cases, a Git repository can be massive, and you may not need all the branches for your work.
Sometimes, you may want to clone only a specific branch, allowing you to work on that branch without affecting other files. It is useful to clone only a specific branch when working on a feature or when fixing a bug that exists only in that branch.
In this tutorial, you will learn to clone a specific Git branch.
Prerequisites
- Git installed (see how to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
Clone Specific Git Branch
The command used to clone a Git branch is git clone
. However, the git clone
command clones all the branches and the remote HEAD
(usually the master
/main
branch).
There are two ways to clone a single Git branch with git clone
:
- Method 1. Clone the entire repository, fetch all the branches, and check out the specified branch after the cloning process.
- Method 2. Clone only a specific branch and no other branches.
The sections below explain both methods so you can use them to your preference.
Method 1 - Fetch All Branches and Checkout One
Use this method to fetch all branches from a repository and then check out a single one. The specified branch becomes the configured local branch for git push and git pull. However, note that the command still fetches all the files from all the branches in the repository.
Use the following syntax to fetch all the branches from the remote repository and check out to the specified one:
git clone --branch [branch_name] [remote-url]
Note: Replace the variables [branch_name]
and [remote-url]
with the correct branch name and remote url.
For example:
In the example above, we fetch all the branches from the specified repository and check out the new-feature
branch. After changing the directory to the one containing the repository, the default branch is new-feature
, not master
.
Method 2 - Fetch Files from Single Branch Only
The second method allows you to fetch only the files from the specified branch, and nothing else. Doing this saves a lot of space on the hard drive and reduces the download size. Since some repositories can be very large, this method reduces bandwidth usage if you need to work on a specific branch only.
Note: In order to fetch only a single branch, Git version 1.7.10 or later is required.
The syntax is:
git clone --branch [branch_name] --single-branch [remote-url]
For example:
The command fetches the specified branch and copies only the files contained on that branch, without fetching any other branches or files. Running git branch
shows which branches were copied.
Conclusion
This tutorial showed how to fetch a single branch from a Git repository. The option is useful when you don't want to download the entire repository, or when you want to work only on a single feature.
For more Git tutorials, see our Git beginner's guide, check out our Git tags tutorial, or the Git submodule guide.