git
version control
remote branches
branch cloning
GitHub

How do I clone all remote branches?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

The most important thing to know is that git clone already brings down the remote branch references. You do not need a special "clone all branches" command just to know that those branches exist.

What git clone does not do is create a local working branch for every remote branch. It checks out one local branch, usually the remote default branch, and leaves the others as remote-tracking branches until you explicitly create local branches for them.

What git clone Actually Gives You

After a normal clone:

bash
git clone https://example.com/repo.git
cd repo

Git has already fetched the repository objects and remote-tracking branches such as:

bash
git branch -r

You might see output like:

text
1origin/HEAD -> origin/main
2origin/main
3origin/release
4origin/feature-x

Those remote branches are already present locally as references. You do not need to clone them one by one.

Create a Local Branch When You Need One

If you want to work on a specific remote branch, create a local tracking branch for it:

bash
git switch --track origin/feature-x

Or, with older syntax:

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

That creates a normal local branch and sets it to track the remote branch.

Fetch New Remote Branches Later

If more branches are created on the remote after your initial clone, update your remote-tracking references with:

bash
git fetch --all --prune

Then list them again:

bash
git branch -r

This keeps your local knowledge of the remote repository current.

If You Really Want Local Branches for Everything

Most of the time, creating local branches for every remote branch is unnecessary clutter. But if you do need that layout, create the local branches intentionally.

For example, create one branch at a time:

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

That is usually clearer than mass-creating dozens of local branches you may never use.

This matters because local branches have their own reflogs, local state, and possible stale history. Remote-tracking branches are often enough until you actually begin work.

Bare Mirrors Are a Different Case

If your goal is not a working checkout but a full mirror of all refs for backup or migration, use --mirror:

bash
git clone --mirror https://example.com/repo.git

A mirror clone is a bare repository. It is not meant for normal development work. It copies all refs in a way that is useful for replication, backup, and repository transfer.

So there are really two separate questions:

  • normal development clone with access to all remote branches
  • complete mirrored repository for administrative purposes

Do not use --mirror if you just want a normal working tree.

Why the Confusion Happens

The confusion usually comes from mixing up three concepts:

  • remote branches on the server
  • remote-tracking branches in your local clone
  • local branches you can commit on directly

A normal clone already gives you the second category. You create the third category only when you need it.

Once that distinction is clear, the behavior of git clone, git branch -r, and git switch --track becomes much easier to understand.

Common Pitfalls

The biggest pitfall is assuming that branches are "missing" just because they do not appear in git branch. That command shows only local branches. Use git branch -r or git branch -a to see remote-tracking branches too.

Another common mistake is creating a local branch for every remote branch automatically. On large repositories, that often makes the local clone noisy and harder to manage.

People also misuse git clone --mirror for day-to-day development. A mirror clone is bare and not meant for normal editing and building.

Finally, remember to fetch again later. Cloning once does not magically keep discovering new branches forever. Use git fetch --all --prune when you want the latest remote branch state.

Summary

  • 'git clone already fetches remote branch references.'
  • A normal clone checks out only one local branch, not one local branch per remote branch.
  • Use git branch -r to see remote-tracking branches.
  • Create a local branch only when you want to work on a specific remote branch.
  • Use git clone --mirror only when you need a full mirrored repository, not a normal working checkout.

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.