The git clone –single-branch –branch command clones a specific branch. This command lets you copy the contents of a repository without downloading all the branches on the repository. It is useful if a repository is large and you only want to download the code you will use.
By default, the git clone command duplicates all the branches from a Git repository. To clone only a specific branch, you must use the –single-branch flag with the git commit command.
In this guide, we discuss how to clone a specific branch using Git using the git clone command. We walk through an example to help you reinforce your learning.
What is Cloning?
Cloning lets you save a copy of a repository hosted elsewhere onto your local machine. You can clone a repository using the git clone command.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The git clone command means that a Git version control server does not need to provide a web interface. You can download a copy of a Git repository from the command line.
Cloning a specific branch is a common way to reduce the impact a repository will have on your available disk space. This is because you will not clone all the branches on the project. You can always download a branch later if you discover you need one you have not downloaded.
Git Clone a Specific Branch
The git clone –single-branch –branch command clones a specific branch from a Git repository. Specify the name of the branch you want to clone after the –branch command. You can always download any other branches you need after you have cloned the repository.
You can limit the branches the clone command retrieves using the –single-branch option:
git clone --single-branch --branch <branch-name> <url>
The <branch-name> denotes where you should specify the name of the branch you want to clone. The branch you clone should exist otherwise this command will return an error. <url> denotes the URL of the repository from which you want to clone a branch.
Git Clone a Specific Branch: Example
We have a repository called ck-git. This repository has two branches: master and dev. We only want to retrieve the master branch because we do not plan on working with the dev branch.
To retrieve only the master branch, we’re going to use the –single-branch option with the git clone command:
git clone --single-branch --branch master https://github.com/career-karma-tutorials/ck-git
After the –single-branch flag, we have specified a value for the –branch flag. This is where we tell Git what branch to clone. Next, we specify the URL of the repository we want to clone, like we would with any git clone command.
Let’s see what happens when we run our command:
Cloning into 'ck-git'... remote: Enumerating objects: 37, done. ... Unpacking objects: 100% (37/37), done.
The git clone command has copied the ck-git repository to our local machine. The command has only cloned the “master” branch because we used the –single-branch flag.
We can verify that only the “master” branch was cloned by navigating into our new project folder and executing the git branch command:
cd ck-git/ git branch
The git branch command lists all of the branches that we have stored locally in our repository:
Only one branch has been cloned. This is the master branch.
You must specify the –single-branch flag if you want to clone a single branch. The –branch flag alone specifies the branch you want to check out when you navigate into a repository. A clone operation with only the –branch flag still fetches all the branches in a repository.
The –single-branch flag is supported from Git version 1.7.10 and in future versions.
Fetch a Remote Branch
Because we have only downloaded one branch, we cannot see the code on other branches in our project.
We have just realized we also need a copy of the “dev” branch. This is not a problem because we can fetch remote branches after initially cloning our repository. We can retrieve the “dev” branch using the git checkout command:
git checkout --track origin/dev
This command will retrieve the dev branch on our “origin”. The “origin” refers to the remote repository with which our repository is associated.
The “dev” branch will be saved to a local branch. Then, our Git HEAD will change to the “dev” branch. This means we’ll move from viewing whatever branch we were on to the “dev” branch.
You can clone a specific branch from a Git repository using the git clone –single-branch –branch command. This command retrieves all the files and metadata associated with one branch. To retrieve other branches, you’ll need to fetch them later on.
Do you want to learn more about Git? Check out our How to Learn Git guide. In this guide, you’ll find a list of top online learning resources, courses, and books. You’ll also find expert advice on how can go from a beginner Git user to being an expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.