List remote Git branches

I used to think git branch -r was the way to go for finding remote branches.

Until today, when I was working on repository I hadn’t updated locally for a while and doing git branch -r gave me some older branch names that I was pretty sure I had deleted (and double checking GitHub confirmed that too).

Moreover shouldn’t git branch -r prompt for my SSH key when connecting to the repository? It doesn’t, so maybe the command’s just returning some cached info?

Turns out the accurate way of querying a remote repository is to use the git ls-remote command:

This does show the correct branches.

A better way to do this is to use the git remote command. This is easier to remember too and the output is more informative:

This command actually runs the git ls-remote command behind the scenes. If you want to see the cached information instead (what git branch -r was looking at) use the -n switch:

To remove the branches that are no longer present locally (stale branches) use the git remote prune command:

To summarize:

  • View current remote branches via git remote show origin
  • Prune stale local branches via git remote prune origin

Of course replace origin appropriately if your remote has a different name. The primary remote repository is usually given a friendly name origin.