Sunday, September 22, 2024
Google search engine
HomeGuest BlogsGit: Clone a Specific Branch

Git: Clone a Specific Branch

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 - tutorialGit: 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.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.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

RELATED ARTICLES

Most Popular

Recent Comments