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
Task | Command |
---|---|
List local branches | git branch |
List remote branches | git branch -r |
List all branches | git branch -a |
List with commit info | git branch -vv |
Update branch info | git 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.