Clone only one branch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Git normally clones the repository metadata and makes all remote branches visible, even if you plan to work on only one of them. If you want a lighter checkout focused on a single branch, use --branch together with --single-branch, and optionally --depth 1 when you only need recent history.
Clone a Specific Branch
The basic command is:
This tells Git to:
- check out
feature-x - fetch only that branch's history by default
If you do not include --single-branch, Git still checks out the requested branch, but the clone may include references and history that you were trying to avoid.
Make the Clone Smaller with Shallow History
If you only need the latest commit history for that branch, add --depth 1:
This is especially useful for:
- large repositories
- CI jobs
- quick inspections
- bandwidth-limited environments
Be aware that shallow clones are intentionally incomplete. Some history-based operations, such as deep blame or searching older commits, will not work until you fetch more history later.
If the repository is especially large, this flag combination can save a surprising amount of time and disk space. It is also common in automation where the runner only needs one branch long enough to build or test it.
Fetch Another Branch Later If Needed
A single-branch clone is not permanent. If later you need more branches, you can fetch them explicitly:
Or remove the restriction by fetching more broadly:
So "clone only one branch" is really a starting optimization, not a one-way door.
Know the Difference Between Checkout and Fetch Scope
This is the common source of confusion:
- '
--branch feature-xchooses what gets checked out initially' - '
--single-branchlimits what Git fetches' - '
--depth 1limits how much history is downloaded'
These flags solve related but different problems. If the goal is smaller download size, --single-branch and often --depth 1 are the important parts.
They also differ from sparse checkout. Sparse checkout limits which files appear in the working tree, while single-branch clone limits which branch data is fetched initially.
Common Pitfalls
The biggest mistake is using only -b feature-x and expecting a minimal clone automatically. That checks out the branch, but it does not by itself guarantee the narrow fetch behavior people usually want.
Another issue is forgetting that shallow clones have limited history. If later commands behave strangely, the missing history may be the reason rather than a broken repository.
Developers also sometimes assume they can never access another branch after a single-branch clone. In reality, they can fetch more branches whenever needed.
Finally, do not confuse a sparse checkout with a single-branch clone. Sparse checkout and shallow clone solve different problems.
Summary
- Use
git clone --branch name --single-branchto clone only one branch by default. - Add
--depth 1when you want a shallow clone with minimal history. - '
--branchselects the initial branch, while--single-branchnarrows the fetch scope.' - You can fetch additional branches later if the need changes.
- Single-branch clone and sparse checkout solve different problems.
- You can deepen a shallow clone later with
git fetch --deepenwhen more history becomes necessary. - A narrow clone is a download optimization, not a permanent restriction on what the repository can contain locally.

