How to Check Branches in Git

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

TaskCommand
List local branchesgit branch
List remote branchesgit branch -r
List all branchesgit branch -a
Fetch latest remote infogit 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.

Sharing Is Caring:

Leave a Comment