How to Clone a Single Remote Branch in Git

John Wachira Feb 15, 2024
  1. Clone a Single GitHub Branch With the git remote add Command
  2. Clone a Branch With the git clone Command
  3. Clone a Single Branch With the git checkout Command
How to Clone a Single Remote Branch in Git

In this article, we will discuss the process of downloading a single branch from GitHub to our local machines.

This comes in clutch when we work as a team on a project to keep our repository up to date with new branches. Let’s jump right in!

Clone a Single GitHub Branch With the git remote add Command

We can use the git remote add command to download a single branch from a remote repository. Let us look at an example.

To simulate a scenario where we want to download a remote branch from a repo, we will create a new branch in our remote repo called Sample_Branch.

new branch

We now have the main branch as the parent branch and Sample_Branch as the child branch.

The next step is to run the git remote add command to fetch the branch from our remote repo. We will run the command as shown below.

$ git remote add -f Sample_Branch https://github.com/Wachira11ke/Delftscopetech.git

Local branch

Now we can use the git checkout command to complete the cloning process. We will run the command below.

$ git checkout -b Sample_Branch
Switched to a new branch 'Sample_Branch' #Output

That’s how we clone a single branch with the git remote add command. Let us now see how we can clone a single branch with the git clone command.

Clone a Branch With the git clone Command

We will delete the Sample_Branch from our local repo and attempt to clone it again using the git clone command.

$ git branch -d Sample_Branch
Deleted branch Sample_Branch (was df90895).

We know that running the git clone command with the --single-branch argument will only clone the master branch. However, we can pass a --branch flag and indicate the name of the branch in the remote repo we would like to clone, as shown below.

$ git clone --single-branch --branch Sample_Branch https://github.com/Wachira11ke/Delftscopetech.git

Clone a Branch with the Git Clone Command

Let us check if the branch is present in our local repository.

$ git branch

Output:

$ git branch
  Sample_Branch
* main

Clone a Single Branch With the git checkout Command

We can use the git checkout command to clone a single branch, as shown below. Again, we will delete the branch from our local repo and download it using the git checkout command.

$ git branch -d Sample_Branch
Deleted branch Sample_Branch (was 216560f).

To clone our remote branch, we will run:

$ git checkout -b Sample_Branch origin/Sample_Branch

Alternatively, we can run:

$ git checkout -t Sample_Branch

The above examples will download the specified remote branch to our local repository.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Branch