Git
Branching
Code Cloning
Software Development
Version Control

How do I clone a single branch in Git?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you only care about one branch from a repository, Git can avoid fetching the other branch histories by cloning with --single-branch. This is useful for large repositories, CI jobs, or cases where you want the checkout to start focused on one branch instead of pulling everything by default.

The Standard Command

bash
git clone --branch develop --single-branch https://example.com/repo.git

This does two things:

  • checks out the develop branch
  • fetches only that branch's history instead of all branches

You can use -b as a shorter form of --branch.

bash
git clone -b develop --single-branch https://example.com/repo.git

What Happens Without --single-branch

If you run only:

bash
git clone -b develop https://example.com/repo.git

Git checks out develop, but it may still fetch the other remote branch refs and history depending on the repository and default behavior. --single-branch is what tells Git to narrow the fetch scope.

That is the flag that makes the clone more selective.

Use Shallow History Too If You Want Speed

If you only need the latest snapshot or a short recent history, combine single-branch cloning with a shallow clone.

bash
git clone -b develop --single-branch --depth 1 https://example.com/repo.git

This is especially useful in CI systems and temporary workspaces.

The tradeoff is that some history-based operations, such as deep blame or long-range diff investigation, become limited until you fetch more history.

Fetching More Later

A single-branch clone is not a permanent prison. You can still fetch other branches later.

bash
git fetch origin main

Or even widen the remote fetch configuration if your use case changes.

So the command optimizes the starting point. It does not prevent future expansion.

Remote Branch Name Must Exist

If the named branch does not exist on the remote, the clone fails.

That is why it is worth confirming the branch name first when working with unfamiliar repositories or automation.

Single Branch Does Not Mean Detached State

After the clone, you normally land on a local branch tracking the chosen remote branch. This is not the same as checking out a raw commit into detached HEAD state.

That makes the workflow natural for normal development: pull, commit, branch, and push still work as expected on that branch.

Useful In Automation

Single-branch clones are often a good default for:

  • build pipelines
  • deployment jobs
  • scripts that inspect one release branch
  • large monorepos where extra history costs time and bandwidth

The command is simple, but the savings can be significant at scale.

Expanding Later Is Normal

If your needs change after the initial clone, you can fetch more history or other branches without recloning the repository. That makes --single-branch a bandwidth and focus optimization, not a permanent limitation on future Git operations.

Common Pitfalls

The biggest mistake is assuming -b branch-name alone always limits the fetched remote branch data. Another is combining --depth 1 with later workflows that quietly expect full history. Developers also sometimes forget that they can fetch more branches later and overcomplicate the initial clone strategy. Finally, if the branch name is wrong or only exists locally on someone else's machine, git clone cannot find it on the remote.

Summary

  • Use git clone -b branch --single-branch <url> to clone one branch selectively.
  • Add --depth 1 if you also want a shallow clone.
  • '-b chooses the branch; --single-branch narrows what is fetched.'
  • The clone can still be expanded later with additional fetches.
  • This pattern is especially useful for large repositories and automation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.