Show Branches

Last updated on

When you work with Git, many times you will be working in multiple branches on a repository. Knowing how to list all branches in Git is a basic skill you need — whether you are moving between features, monitoring your progress, or collaborating with others. In this tutorial, I will show you the different commands and options that are available for listing local and/or remote branches in your Git repository. At the end of this post, you should be able to quickly tell which branches are what and how that can easily enhance your workflow. Let’s dive in!

Command Syntax for Listing Git Branches

git branch

This command simply lists all the local branches in your Git repository. The branch currently in use is marked with an asterisk.

For example:

git show branches

To list branches matching a specific pattern, use --list followed by the first slug name of the branch.

git branch --list 'collected*'

There are other commands used to list branches on the remote server, and additional flags can perform various other tasks. Let’s see how to list all branches locally and remotely in Git.

Listing All Branches Locally and Remotely in Git

By running the following command, you will see all branches locally, and remotely.

git branch -a

This command combines information from local branches (git branch) and remote branches (git branch -r), so you get a comprehensive list of all the Git repository in your Gits. Above output detail about, the branch you’re currently checkout with an asterisk.

show all branches remote local

One more thing, you can display more data about each branch using the -vva flag.

git branch -vva

This command is used to display more detailed information about branches, including both local and remote branches, along with additional details such as the last commit on each branch.

Show branches -vv

Let’s focus a bit more on how to list the branches in the remote repository.

Viewing Remote Branches: Displaying Remote Branches

Listing Git Branches Remotely lists the branches that exist on a remote-only repository, generally where you only use the including server as an archive. This last fact is key in collaborative development, where several developers work together on a project and need to keep tabs on the branches available in their remote repository. The command is very cool and the primary one used for this which git branch –remote or -r.

Let’s see an example.

git branch -r

Alternatively, to list all remote branches you can use the following command.

git branch --remote

# OR

git branch --remotes
Bash

Running any of these two commands will return the screen as shown in the image below.

git remote branches

Developers sometimes use git ls-remote it to visually see the branch references on a remote repository. So let us go through this command and see how it works.

Listing Remote Heads by Branch References

The command of git ls-remote --heads origin provides targeted searching of remote branch references in a repository named ‘origin’. Subheading again stresses what command provides and why it exists, to return extensive information about branch references (with a bonus of not needing a local repository for that) For example:

# list the references (usually branches) 
# on a remote repository
git ls-remote --heads


# Fetches information about all types of 
# references in the remote repository.
git ls-remote 

The result of both commands would look like the image below.

list all branches in git

The refs/heads/master and refs/heads/cars are the branches on a remote repository that you have in the above image.

By scrolling down to the section below, we will explain why we ran the list branches command. Let’s move forward.

Project Collaboration and Branch Management

If you are working with others on a project or managing different features, reading all branches in the repository accurately is important. Branches listing: to check what kind of parallel streams are going on in the project. This information is useful for a variety of reasons:

  • Project Structure Overview
  • Navigating Between Features
  • Monitoring Progress
  • Collaboration
  • Branch Cleanup
  • Troubleshooting

Let’s summarize it.

Wrapping Up

In this tutorial, we explored the fundamental aspects of list branches in a Git repository, both locally and remotely.

The exploration of the git branch command started with basic: Git comes built-in with a nice little shorthand that lets us list out all our local branches very easily. In addition, we showed pattern matching via flags –list to show how you could filter and organize which branches are displayed based on certain criteria.

Broadening our horizons, we looked at the git branch -a command which gives an overview of both local as well as remote branches. The complete list helps the developer to have a quick look at the branch’s landscape and focus on the currently checked-out branch is more readable.

If you wanted to see a little more detail than that, we introduced the -vva flag which is quite useful as it provides even further information about each branch including details of the last commit. This was especially valuable for developers who wanted to learn more about the recent history of branches.

Switching back to remote repositories, we examined the importance of remote-tracking branches in a collaborative development team. The git branch -r command which became the go-to utility for viewing all remote branches without much hassle.

We also presented alternative commands like git branch --remote and git branch --remotes to provide developers with options on what command they want to use in accessing details of branches within a remote server.

In a dedicated series, We have looked at the git ls-remote --heads origin command to show you how it can be used again providing an alternative way of exploring branch references in our remote repository. This is especially convenient for situations where you need details but there’s no local repository available.

Frequently Asked Questions (FAQs)

  • How do you list all local branches in a Git repository?

    You can easily list all the local branches in a Git repository by executing Git branch. The one that will be with an * is the branch we are currently using.

  • How can you list branches that match a specific pattern in Git?

    To list only those branches that match a pattern, run git branch --list 'pattern', where ‘pattern’ is the slug for which you want to find matching branches.

  • What command do you use to list all branches, both locally and remotely, in Git?

    You can run git branch -a to list all branches, both on your local machine and remote.

  • How can you display more detailed information about branches in Git?

    Use git branch -vva to get more details about branches, such as the last commit on each of them.

  • What command is used to list remote branches in Git?

    To see branches that are located in remote repository use this command: git branch -r or git branch --remote.

  • What does the git ls-remote --heads origin command do?

    The git ls-remote --heads origin command will display the branch references in a remote repository named, just an exampled as ‘origin’ but it’s from only information nothing to do with your local clone of that repo for which you run this.

Share on: