Git
How to List All Branches in Git?
Git, the powerful version control system, allows developers to work on multiple features or bug fixes simultaneously using branches. As a project grows, it’s common to manage several branches, both locally and remotely. Knowing how to list all branches helps you navigate through the repository efficiently.
In this blog, we’ll explore various Git commands to list all branches, differentiate between local and remote branches, and provide tips for better branch management.
What Are Branches in Git?
In Git, a branch is essentially a pointer to a particular commit in the repository.
- Local branches are stored on your machine and are used for development.
- Remote branches exist on a remote repository (e.g., GitHub, GitLab) and are used to share code among team members.
Knowing how to view and manage these branches is essential for a clean workflow.
How to List All Branches in Git
1. List Local Branches
To display all local branches in your repository, use the following command:
git branch
This will show a list of local branches, with the current branch marked by an asterisk *
.
Example Output:
* main
feature/login
bugfix/crash-fix
2. List Remote Branches
To display all remote branches that exist on the remote repository, use:
git branch -r
Example Output:
origin/main
origin/feature/login
origin/bugfix/crash-fix
Here, origin
is the default name for the remote repository.
3. List Both Local and Remote Branches
To see all branches, including local and remote branches, use:
git branch -a
Example Output:
* main
feature/login
bugfix/crash-fix
remotes/origin/main
remotes/origin/feature/login
remotes/origin/bugfix/crash-fix
Branches starting with remotes/origin/
indicate remote branches.
4. List Branches with the Last Commit Details
If you want to see branches along with the last commit made on each branch, use:
git branch -v
Example Output:
* main a1234cd Fix typo in README
feature/login b5678ef Add login form validation
bugfix/crash-fix c9101gh Fix app crash on startup
5. List Merged Branches
To list branches that have already been merged into the current branch, run:
git branch --merged
Example Output:
* main
feature/login
This is useful for identifying branches that are safe to delete.
6. List Unmerged Branches
To see branches that haven’t been merged into the current branch, use:
git branch --no-merged
Example Output:
bugfix/crash-fix
This helps identify branches that still require work or review.
Bonus: How to List Remote Branches Using git ls-remote
If you need to fetch a list of all branches directly from a remote repository without cloning it, use the git ls-remote
command:
git ls-remote --heads <remote_repository_url>
Example Output:
a1234cd refs/heads/main
b5678ef refs/heads/feature/login
Here, the output includes commit hashes and branch names.
Tips for Managing Branches in Git
- Clean Up Stale Branches:
Usegit branch -d <branch_name>
to delete merged local branches andgit push --delete origin <branch_name>
for remote ones. - Fetch the Latest Branches:
Rungit fetch --prune
to sync your remote branch list and remove outdated references. - Name Branches Clearly:
Follow naming conventions likefeature/<name>
orbugfix/<name>
for clarity. - Use Git GUI Tools:
Tools like GitHub Desktop, Sourcetree, and GitKraken provide a visual representation of branches, making navigation easier.
Summary
Listing branches in Git is a fundamental skill that helps you navigate and manage your repository effectively. Here’s a quick recap of the commands:
git branch
– List local branches.git branch -r
– List remote branches.git branch -a
– List all branches.git branch -v
– View branches with last commit details.git branch --merged
– List merged branches.git branch --no-merged
– List unmerged branches.
By mastering these commands, you can maintain a clean workflow, collaborate efficiently, and keep your repository organized.