When working with Git, especially in collaborative environments, it’s important to keep track of the various branches in your repository. Whether you’re switching branches, cleaning up old ones, or reviewing your team’s work, knowing how to list all branches โ both local and remote โ is essential.
This quick guide will show you how to display all Git branches using a few simple commands.
๐ List Local Branches
To see all branches that exist locally on your machine:
git branch
Example Output:
* main
feature/login
bugfix/api-error
- The
*
indicates the currently checked-out branch. - These are the branches youโve created or checked out locally.
๐ List Remote Branches
To view branches that exist on the remote repository (e.g., GitHub or GitLab):
git branch -r
Example Output:
origin/HEAD -> origin/main
origin/feature/login
origin/bugfix/api-error
This shows the branches available on the remote, typically prefixed with origin/
.
๐ List All Branches (Local + Remote)
To display all branches โ both local and remote โ in one list:
git branch -a
Example Output:
* main
feature/login
remotes/origin/main
remotes/origin/feature/login
This is especially useful for getting a complete picture of your repositoryโs structure.
๐ Keep Remote Branches Updated
To make sure youโre seeing the latest remote branches:
git fetch --all
This updates your local copy of the remote branch references, ensuring you can view the most current state of the repository.
๐งน Optional: Prune Deleted Remote Branches
To remove references to branches that have been deleted from the remote:
git remote prune origin
This helps keep your remote branch list clean and accurate.
โ Summary
Command | Description |
---|---|
git branch | List local branches |
git branch -r | List remote branches |
git branch -a | List all branches (local + remote) |
git fetch --all | Update remote branch information |
git remote prune origin | Clean up deleted remote branches |
Conclusion
Understanding how to view all branches in Git is fundamental for navigating any Git-based workflow. Whether you’re merging, switching, or cleaning up branches, these commands will give you full visibility into your project’s structure.