Git: Clone a Specific Branch

April 6, 2023

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.

Git: How to clone a specific branch - tutorial

Prerequisites

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:

Cloning a repository and checking out a specific Git branch.

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:

Cloning a single branch in Git.

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.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Git Merge Master into Branch
March 22, 2023

This tutorial shows two methods for merging the master branch into another branch in Git. See examples and use cases.
Read more
How to Use git submodule init
September 28, 2022

The git submodule init command adds submodule entries to the local Git configuration file. This tutorial shows you how to use git submodule init with common usage examples.
Read more
How to Pull All Branches in Git
March 16, 2023

Branches in Git are independent development lines in a repository. This tutorial shows how to pull all branches from a remote repository into a local one in Git.
Read more
SSH vs. HTTPS for Git: Which One to Use?
February 21, 2023

This article compares and gives advice on choosing between SSH and HTTPS - two network protocols used to secure the connection to remote repositories.
Read more