The act of listing branches in Git, a common operation to view available branches in your repository, involves the practice of retrieving and displaying a list of branches existing in a Git repository. In Git, a branch represents a separate line of development that empowers developers to work on distinct features, bug fixes, or experiments independently of each other.

Let’s move to the syntax of listing Git branches.

Command Syntax for Listing Git Branches

git branch
Bash

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 branch

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

git branch --list 'collected*'
Bash

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

To see a list of branches, both locally and remotely, execute the following command:

git branch -a
Bash

This command amalgamates information about local branches (git branch) and remote branches (git branch -r), offering a complete overview of all branches in your Git repository. The output includes details about the currently checked-out branch, marked with an asterisk.

Show All Remote and Local Branches

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

git branch -vva
Bash

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.

display more detailed information about branches

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

Listing Git Branches Remotely

Listing Git branches remotely involves retrieving information about branches that exist on a remote repository, typically hosted on a server. This is crucial for collaborative development, where multiple developers contribute to a project and need to be aware of the available branches on the remote repository. The primary command used for this purpose is git branch followed by --remote or -r.

Let’s see an example.

git branch -r
Bash

Alternatively, you can also use the following command to show all remote branches:

git branch --remote

# OR

git branch --remotes
Bash

Executing either of these two commands will produce the same result as shown in the image below.

git show branches remotely

Sometimes, developers like to list the branch references on a remote repository using git ls-remote. Let’s see how this command works.

Listing Remote Heads by Branch References

The command git ls-remote --heads origin offers a focused exploration of branch references on a remote repository named ‘origin.’ This subheading emphasizes the specific functionality and purpose of the command, highlighting its role in providing detailed information about branch references without the need for a local repository. 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 
Bash

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

git ls-remote command

In the above image, refs/heads/master and refs/heads/cars are the branches on the remote repository.

By moving into the section below, you will understand why we are using the ‘list branches’ command. Let’s move forward.

Project Collaboration and Branch Management

When working on a project with multiple collaborators or when managing different features, it’s crucial to know what branches are present in the repository. Listing branches provides a quick overview of the parallel streams of work happening within the project. This information is valuable for several reasons:

  • Understanding Project Structure
  • 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.

We initiated our exploration by introducing the basic git branch command, which effectively lists all local branches. With the addition of flags like --list for pattern matching, we demonstrated how to filter and organize the displayed branches based on specific criteria.

Expanding our scope, we examined the git branch -a command, providing a unified view of both local and remote branches. This comprehensive listing offers developers a quick snapshot of the project’s current branch landscape, highlighting the currently checked-out branch for clarity.

For those seeking an even more detailed overview, we introduced the -vva flag, a powerful tool that reveals additional information about each branch, including the last commit details. This proved valuable for developers keen on understanding the recent history of branches.

Shifting our focus to remote repositories, we explored the significance of listing branches remotely, emphasizing its critical role in collaborative development scenarios. The git branch -r command emerged as a primary tool, efficiently displaying all remote branches.

Additionally, we showcased alternative commands, such as git branch --remote and git branch --remotes, offering flexibility in how developers choose to access information about branches on the remote server.

In a specialized section, we introduced the git ls-remote --heads origin command, highlighting its role in providing a focused exploration of branch references on a remote repository. This command is particularly useful for obtaining detailed information without the need for a local repository.

For more tutorials, visit our full collection of Git tutorials here.