How to git clone a specific branch


Command to clone a specific Git branch

To git clone only a single, specific Git branch, issue this command:

git clone --single-branch --branch development

The steps to clone a specific Git branch differ from a typical clone command in two ways:

  1. The –single-branch (two dashes) flag limits the clone to only one branch
  2. The –branch (two dashes) flag specifies which specific Git branch to clone

Single git branch clone example

Here is an example of clone of a single branch from a GitHub repo.

git-clone@branch:~$ git clone --single-branch --branch development 
* development

Notice how a request to list all branches after a single Git branch has been cloned only shows:

  • A local copy of the specific branch Git cloned
  • A single branch listed as a remote reference
git-clone@branch:~$ git branch --all
* development

This proves that indeed the command only did a clone of a single, specific Git branch on the remote server.

git clone single specific branch

When you clone a specific Git branch, you cannot check out other branches.

Multiple vs single git branch clone commands

Contrast the output above to the result if a normal clone operation was performed.

git-clone@branch:~$git clone 
* master
remotes/origin/HEAD -> origin/master

As you can see, without parameterization, a normal git clone command will make the default main or master branch local, and maintain remote references to other branches that the developer can checkout or switch to at will.

Consequences of a single git branch clone

For CI/CD pipelines built with tools like Jenkins, GitHub Actions or TeamCity, it makes sense to only clone a single, specific Git branch.

However, for day-to-day development operations the consequences of cloning only a single branch will make merging code and switching between branches more difficult.

After you git clone a specific branch, your local copy of the remote repo:

  • Will not contain any local copies of the remote branches
  • Will not contain any references to other branches in the remote repo

Pathspec errors: master not known to Git

For example, in this single branch clone example, we have not copied the main or master branch.

Any attempts to check out the main or master branch will trigger a pathspec error telling us the branch does not exist locally, despite the fact that it does indeed exist on the remote repo we cloned.

git-clone@branch:~$ git checkout master
error: pathspec 'master' did not match any file(s) known to git

git-clone@branch:~$ git checkout main
error: pathspec ‘main’ did not match any file(s) known to git

To fix the pathspec error, the developer will need to either:

  • Add remote references to the branches they need
  • Start over with a full clone of the repository

Git clone a single branch with a specific depth

It is a common requirement of a continuous integration build to only clone a single, specific Git branch.

After all, if all you’re doing is compiling the latest commit in the development branch, why clone every other branch in the process?

If this is the case, you might also want to limit the git clone depth.

Specific clone depth of 1

If you set the clone depth to 1, you’ll only get the latest commit, not the entire history of the project.

It’s a nice feature that might save your Jenkins server or GitHub Actions routine some clock-cycles.

It’ll save your CI/CD pipeline servers some hard drive space.


App Architecture
Software Quality
  • How to test a predictive model

    Strategies for testing predictive models and analytics emphasize data quality, real-time testing and code redundancy, as well as ...

  • The dos and don'ts of visual testing

    The visual aspect of an application is an important part of UX. Defects can potentially result in lost sales and damaged ...

  • 3 QA testing tools to consider

    QA testers need to be able to put applications and APIs through their paces. Here are some examples of tools that can help with ...

Cloud Computing