Git
How to List Branches in Git?
Branches in Git are a core feature that allows developers to work on different features, bug fixes, or experiments simultaneously without impacting the main codebase. Managing branches effectively requires knowing how to list them, whether locally or remotely.
This blog explains how to list branches in Git, including commands and tips to streamline your workflow.
Understanding Git Branches
What Are Git Branches?
Git branches are pointers to specific commits in a repository’s history. By default, Git initializes a repository with a single branch, typically called main
or master
. New branches allow developers to work on separate tasks or features independently.
Types of Branches
- Local Branches: Branches that exist only on your local machine.
- Remote Branches: Branches that exist on a remote repository, like GitHub or GitLab.
Listing Branches in Git
1. List Local Branches
To view all branches on your local machine:
git branch
Example Output:
* main
feature-1
bugfix-123
- The
*
indicates the branch you are currently on.
2. List Remote Branches
To view all branches available in the remote repository:
git branch -r
Example Output:
origin/main
origin/feature-1
origin/bugfix-123
3. List All Branches (Local and Remote)
To list both local and remote branches together:
git branch -a
Example Output:
* main
feature-1
remotes/origin/main
remotes/origin/feature-1
remotes/origin/bugfix-123
Understanding the Output
Local Branches
Local branches are shown without any prefixes.
Remote Branches
Remote branches are prefixed with remotes/
followed by the remote name (e.g., origin
) and branch name.
Practical Examples
Example 1: Identify the Current Branch
The current branch is marked with an asterisk (*
) in the output of git branch
.
Example 2: Check for Stale Remote Branches
Sometimes remote branches listed with git branch -r
may no longer exist. Use the following to clean up stale branches:
git remote prune origin
Example 3: Filter Branch Names
Use the grep
command to filter branches:
git branch | grep feature
This filters and displays only branch names containing “feature.”
Advanced Techniques
Viewing Last Commit on Each Branch
To see the latest commit message and author for each branch:
git branch -v
Example Output:
* main 12a4d5e Update README
feature-1 56b8a7c Add new feature
bugfix-123 ab12345 Fix login bug
Viewing Merged Branches
To list branches that have been merged into the current branch:
git branch --merged
Viewing Unmerged Branches
To list branches that have not been merged into the current branch:
git branch --no-merged
Best Practices for Managing Branches
- Use Descriptive Names: Name branches clearly to reflect their purpose, such as
feature-login
orbugfix-issue123
. - Delete Merged Branches: Clean up branches after merging to avoid clutter:
git branch -d branch-name
- Sync Regularly: Fetch updates from the remote repository to keep your local branch list up to date:
git fetch
- Work in Separate Branches: Always work on separate branches for new features or fixes to keep the
main
branch stable.
Conclusion
Listing branches in Git is a simple yet powerful task that helps you stay organized and informed about your repository’s state. By using commands like git branch
, git branch -r
, and git branch -a
, you can effectively manage both local and remote branches.
Adopting best practices for branch management ensures a clean and collaborative development workflow.