0
0
GitHow-ToBeginner · 3 min read

How to List Remote Branches in Git: Simple Commands

To list remote branches in Git, use the command git branch -r. This shows all branches stored on the remote repository. Alternatively, git ls-remote --heads lists remote branches with their commit hashes.
📐

Syntax

The main commands to list remote branches are:

  • git branch -r: Lists all remote branches by name.
  • git ls-remote --heads [remote-name]: Lists remote branches with their commit hashes.

Here, remote-name is usually origin, the default remote.

bash
git branch -r
git ls-remote --heads origin
💻

Example

This example shows how to list remote branches using git branch -r and git ls-remote --heads. It demonstrates the difference between just branch names and branch names with commit hashes.

bash
$ git branch -r
  origin/HEAD -> origin/main
  origin/feature-x
  origin/main

$ git ls-remote --heads origin
b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8g9h0i1	refs/heads/feature-x
c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7g8h9i0j1	refs/heads/main
Output
origin/HEAD -> origin/main origin/feature-x origin/main b1c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8g9h0i1 refs/heads/feature-x c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7g8h9i0j1 refs/heads/main
⚠️

Common Pitfalls

Some common mistakes when listing remote branches include:

  • Running git branch without -r only shows local branches, not remote ones.
  • Not specifying the remote name with git ls-remote can cause confusion if multiple remotes exist.
  • Assuming remote branches are automatically updated locally; you may need to run git fetch first.
bash
$ git branch
* main
  feature-x

# This shows only local branches, not remote.

$ git fetch
$ git branch -r
  origin/HEAD -> origin/main
  origin/feature-x
  origin/main
Output
main feature-x origin/HEAD -> origin/main origin/feature-x origin/main
📊

Quick Reference

Here is a quick cheat sheet for listing remote branches in Git:

CommandDescription
git branch -rList all remote branches by name
git ls-remote --heads originList remote branches with commit hashes
git fetchUpdate remote branch info locally
git branchList local branches only

Key Takeaways

Use git branch -r to see all remote branches by name.
Use git ls-remote --heads origin to see remote branches with commit hashes.
Run git fetch to update remote branch info before listing.
git branch alone shows only local branches, not remote ones.
Always specify the remote name if you have multiple remotes.