Git
Remote Branches
Programming
Version Control
Git 1.7+

How do I list all remote branches in Git 1.7+?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

To list the remote-tracking branches your local repository knows about, use git branch -r. That is the direct answer, but it only shows what your local clone has already fetched, not necessarily the live state of the server this second.

That distinction matters because many "missing branch" questions are really stale-ref questions. The right workflow is usually fetch first, then list.

The Basic Command

This shows remote-tracking branches:

bash
git branch -r

Typical output looks like this:

text
1origin/HEAD -> origin/main
2origin/main
3origin/feature/login
4origin/release/1.2
5upstream/main

These names are references stored in your local repository. They mirror remote branches from remotes such as origin or upstream.

Fetch Before You Trust the List

If the list looks outdated, fetch first:

bash
git fetch --all --prune
git branch -r

Why both flags matter:

  • '--all fetches from every configured remote'
  • '--prune removes remote-tracking refs for branches deleted on the server'

Without a recent fetch, git branch -r may omit newly created remote branches or still show deleted ones.

See Local and Remote Branches Together

If you want the full picture, local plus remote-tracking branches, use:

bash
git branch -a

That helps when you are trying to answer questions like:

  • do I already have a local branch for this remote branch
  • is this branch only remote, only local, or both

It is also a good quick check before creating a new branch name that might already exist remotely.

Query the Remote Server Directly

Sometimes you want to see what the server advertises right now without relying on local tracking refs. In that case, use git ls-remote:

bash
git ls-remote --heads origin

This contacts the remote and prints the branch heads directly. It is especially useful when:

  • you suspect your local refs are stale
  • you do not want to update local tracking refs yet
  • you are scripting or auditing remote state

The output includes commit hashes, so it is more raw than git branch -r, but it is authoritative for that remote.

Work on a Remote Branch

Once you see the branch you want, create a local branch that tracks it:

bash
git switch --track origin/feature/login

If your Git version does not have switch, the older form is:

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

That creates a local branch and sets upstream tracking so later git pull and git push behave as expected.

Understand What origin/HEAD Means

Many users notice a line like this:

text
origin/HEAD -> origin/main

That is not a normal development branch. It is a symbolic reference that points to the default branch of the remote. Seeing it in the list is normal.

If the default branch changes on the server and your local clone still points at the old one, a fresh fetch often updates it. In awkward cases, you can reset it explicitly:

bash
git remote set-head origin --auto

Filter the Output

For large repositories, simple filtering helps:

bash
git branch -r | grep 'release/'
git branch -r | grep -v 'HEAD'

You can also inspect one remote only:

bash
git branch -r | grep '^  origin/'

That keeps the output manageable without changing repository state.

Common Pitfalls

The most common mistake is assuming git branch -r hits the network. It does not. It only prints the remote-tracking refs already stored in your local repository.

Another common issue is forgetting --prune. Deleted remote branches can linger locally and make the branch list look wrong.

Users also confuse remote-tracking refs with real local branches. origin/feature/login is not a branch you edit directly. It is a reference to the remote state.

Finally, do not assume there is only one remote. Repositories that use both origin and upstream are common, and git branch -r will show refs for both.

Summary

  • Use git branch -r to list remote-tracking branches known locally.
  • Fetch first, ideally with git fetch --all --prune, if you want an up-to-date view.
  • Use git branch -a when you want local and remote refs together.
  • Use git ls-remote --heads origin when you want to query the remote server directly.
  • Create a local tracking branch before you start work on a remote branch.

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.