How do I fetch only one branch of a remote Git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Git can fetch just one branch when you ask for an explicit refspec or configure the remote to track only that branch. The right command depends on whether you are cloning a new repository or narrowing an existing local repository that already knows about many remote branches.
New Clone: Use --single-branch
If you are starting from scratch, the cleanest option is to clone only the branch you care about.
This creates a local clone focused on main. According to the official git clone documentation, --single-branch causes future fetches in that clone to update only the remote-tracking branch used during the initial clone.
If you also want less history, combine it with --depth:
That is common in CI jobs or large repositories where downloading every branch and full history is unnecessary.
Existing Repository: Fetch One Branch Explicitly
If the repository already exists locally, you can fetch only the branch you want by naming it on the command line.
Git's fetch documentation says that explicit refspecs on the command line determine what gets fetched. In practice, this is enough for many one-off cases where you just want the latest state of one remote branch.
If you want to control where the fetched branch lands locally, use an explicit refspec:
This makes the mapping explicit and is useful when you want predictable remote-tracking refs.
Track Only One Branch On A Remote
For repeated use, change the remote configuration so git fetch origin only updates one branch instead of all branches.
First inspect the current configuration:
Then replace the default "all branches" refspec with one branch:
After this, a normal git fetch origin updates only origin/main.
This is the persistent answer when the question is really, "How do I stop this repository from tracking every branch on the remote?"
Checking Out The Fetched Branch
Fetching a branch does not automatically create or switch your local working branch. After fetching, create a local branch that tracks the remote branch if needed:
Or with older Git syntax:
fetch updates refs. switch or checkout changes your working branch. Keeping those two steps separate avoids confusion.
One-Off Inspection Without Permanent Tracking
Sometimes you only want to inspect a remote branch temporarily. You can fetch it and inspect FETCH_HEAD without permanently tracking every remote branch.
This is useful when reviewing a branch from another repository or confirming a remote state without changing the local remote configuration.
Clone Versus Fetch
A common mistake is mixing up these two questions:
- "How do I clone only one branch?" Use
git clone --single-branch --branch ... - "How do I fetch only one branch into an existing clone?" Use
git fetch origin <branch>or changeremote.origin.fetch
Those are related but not identical workflows.
Tags And Other Extra Data
Even if you fetch a single branch, Git may still fetch tags related to the objects being downloaded unless you configure otherwise. If your goal is a minimal working copy, consider:
For long-term configuration on a remote:
Do this only if you are sure your workflow does not rely on tags for builds or releases.
Common Pitfalls
- Using
git fetch originwith the default remote config and expecting it to ignore other branches. - Confusing a one-time fetch of one branch with permanently reconfiguring the remote.
- Assuming
git fetchalso checks out the branch into the working tree. - Forgetting that a shallow clone with
--depthis different from a full clone with only one tracked branch. - Changing
remote.origin.fetchwithout checking whether other tooling depends on tracking multiple branches.
Summary
- For a new clone, use
git clone --branch <name> --single-branch <url>. - In an existing repository,
git fetch origin <branch>is the simplest one-off solution. - For persistent single-branch tracking, replace
remote.origin.fetchwith a branch-specific refspec. - Fetching updates refs;
switchorcheckoutupdates your working branch. - Use
--no-tagsonly if you intentionally want a narrower fetch footprint.

