How to List All Branches in Git: Local and Remote

When working with Git, managing branches is an essential part of collaboration and version control. Whether you’re switching between features or reviewing old work, itโ€™s useful to know how to list all branches in a repository.

This post will walk you through how to list local, remote, and all branches in Git.


๐Ÿ”น List Local Branches

Local branches exist on your machine. To see all local branches:

git branch

This will output something like:

  feature-xyz
* main
  bugfix-123
  • The asterisk (*) indicates the current active branch.
  • These are branches youโ€™ve checked out or created locally.

๐Ÿ”น List Remote Branches

Remote branches exist on a remote repository like GitHub or GitLab. To list them:

git branch -r

Output example:

  origin/HEAD -> origin/main
  origin/main
  origin/feature-abc
  • These branches are tracked from the remote repo.
  • origin is the default name for the remote (can vary).

๐Ÿ”น List All Branches (Local + Remote)

To list both local and remote branches:

git branch -a

Example output:

  feature-xyz
* main
  bugfix-123
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/feature-abc

This view is helpful for seeing the full branch landscape of your project.


๐Ÿ”„ Sync Remote Branch List

Sometimes, remote branches are deleted or renamed but still show up. To update your list:

git fetch --prune

This removes references to branches that no longer exist on the remote.


๐Ÿ” Show More Branch Details

To include last commit info:

git branch -vv

This shows the branch, its upstream tracking branch, and the latest commit on each.


๐Ÿงผ Clean Up Stale Local Branches (Optional)

List local branches that no longer have a remote counterpart:

git remote prune origin --dry-run

Then clean them if needed:

git remote prune origin

Summary

TaskCommand
List local branchesgit branch
List remote branchesgit branch -r
List all branchesgit branch -a
List with commit infogit branch -vv
Update branch infogit fetch --prune

Knowing how to list and interpret Git branches is crucial for clean collaboration and efficient development. These simple commands will help you navigate your project with confidence.

Sharing Is Caring:

Leave a Comment