Git
Git Branch
Remote Repository
Fetch
Version Control

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.

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

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:

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

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.

bash
git fetch origin feature-x

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:

bash
git fetch origin refs/heads/feature-x:refs/remotes/origin/feature-x

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:

bash
git remote show origin
git config --get-all remote.origin.fetch

Then replace the default "all branches" refspec with one branch:

bash
git config --unset-all remote.origin.fetch
git config remote.origin.fetch +refs/heads/main:refs/remotes/origin/main
git fetch origin

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:

bash
git switch --track -c feature-x origin/feature-x

Or with older Git syntax:

bash
git checkout -b feature-x origin/feature-x

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.

bash
git fetch https://example.com/repo.git feature-x
git log --oneline FETCH_HEAD

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 change remote.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:

bash
git fetch --no-tags origin main

For long-term configuration on a remote:

bash
git config remote.origin.tagOpt --no-tags

Do this only if you are sure your workflow does not rely on tags for builds or releases.

Common Pitfalls

  • Using git fetch origin with 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 fetch also checks out the branch into the working tree.
  • Forgetting that a shallow clone with --depth is different from a full clone with only one tracked branch.
  • Changing remote.origin.fetch without 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.fetch with a branch-specific refspec.
  • Fetching updates refs; switch or checkout updates your working branch.
  • Use --no-tags only if you intentionally want a narrower fetch footprint.

Course illustration
Course illustration

All Rights Reserved.