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.
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:
Typical output looks like this:
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:
Why both flags matter:
- '
--allfetches from every configured remote' - '
--pruneremoves 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:
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:
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:
If your Git version does not have switch, the older form is:
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:
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:
Filter the Output
For large repositories, simple filtering helps:
You can also inspect one remote only:
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 -rto 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 -awhen you want local and remote refs together. - Use
git ls-remote --heads originwhen you want to query the remote server directly. - Create a local tracking branch before you start work on a remote branch.
Related reading
- How do I list all remote branches in Git 1.7?
- How do I list all the files in a commit?
- How do I make a branch point at a specific commit?
- How do I make a Git commit in the past?
- How do I make git-svn use a particular svn branch as the remote repository?
- How do I make Git forget about a file that was tracked, but is now in .gitignore?
- How do I make Git ignore file mode chmod changes?
- How do I make git use the editor of my choice for editing commit messages?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.