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.
The git clone command is convenient because it means that a Git version control server does not need to provide a web interface for you to retrieve and set up a local version of a repository. You can download a copy of a Git repository from the command line.
When you clone a repository, all the files from the repository you are retrieving will be downloaded to your local machine. Metadata, such as a reference of all of the commits made to a repository, is also stored on your local machine.
This metadata is important because it lets you navigate through the entire history of a project. Using this metadata, you can refer back to previous commits using the git checkout command, see how files have changed using the git log command, and otherwise see how a project has evolved over time.
Cloning a specific branch is a common way to reduce the impact a repository will have on your available disk space if the repository you want to clone a repository is large.
Git: Clone a Specific Branch
The git clone command retrieves all of the files on all the branches in a repository. This makes sense because most people want to create an exact copy of a repository on their local machine.
You can limit the branches the clone command retrieves using the –single-branch option.
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:
We can see that 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 of 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
We now need a copy of the dev branch so we can continue our work on our project. We can retrieve this 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 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 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.
Now you have the knowledge you need to clone a specific branch in Git like a pro!