Branches in Git allow you to work on different features or fixes independently without affecting the main codebase. Knowing how to list and check branches is fundamental to effective Git usage.
This guide covers how to view local, remote, and all branches in a Git repository.
π Check Local Branches
To list all branches that exist on your local machine, use:
git branch
This will output a list like:
* main
feature/login
bugfix/issue-42
- The
*
indicates the currently checked-out branch.
π Check Remote Branches
To see branches on the remote repository (e.g., GitHub or GitLab):
git branch -r
Example output:
origin/main
origin/develop
origin/feature/new-api
These are branches stored on the remote (usually named origin
).
π Check All Branches (Local + Remote)
To list all branches (both local and remote):
git branch -a
Youβll see both local and remote branches together:
* main
develop
remotes/origin/main
remotes/origin/feature/login
π Fetch Latest Remote Branches
Before listing remote branches, it’s good practice to fetch the latest updates:
git fetch
This ensures you see any new branches created by collaborators.
π Bonus: See More Details About Branches
To see detailed info (like last commit and author), use:
git show-branch
Or for a graphical view (if you have git log
skills):
git log --oneline --graph --all
β Summary
Task | Command |
---|---|
List local branches | git branch |
List remote branches | git branch -r |
List all branches | git branch -a |
Fetch latest remote info | git fetch |
π Conclusion
Checking branches in Git is a simple yet powerful way to stay oriented in your project. Whether you’re managing multiple features or collaborating with a team, knowing your branches is essential for clean and organized development.