How to List Branches in Git?

Featured image for listing the branches in Git

The command to list all branches in local and remote repositories:

$ git branch -a

If you require only listing the remote branches from Git Bash then use this command:

$ git branch -r

You may also use the show-branch command to see the branches and their commits as follows:

$ git show-branch

As working with the Git version control system, we have to work with branches. Our repositories may contain several branches and sometimes it is hard to remember all – whether on the local or remote repo.

So, it becomes quite handy to know the way of listing all branches in the repo which helps in switching to the appropriate branch as well.

In the next section, I will show you an example of a remote and local repository.

I will create a few branches in both repositories and then use the commands to list all branches as well as branches in the remote repository only with screenshots.

The example of showing branches in Git

For our example, I have created a few branches in local as well as remote repositories.

The following commands are used for creating the local branches:

$ git branch br-tst1

$ git branch bt-tst2

$ git branch br-tst3

This is followed by creating remote branches:

$ git push origin br-tst1

$ git push origin br-tst3

So, we have three local and two remote branches apart from the master branch in both repositories.

Showing all branches example

For listing all branches – in local and remote repositories, run this command in the terminal:

$ git branch -a

The result is shown in the graphic below:

Git list branches all

The branches in white are the local branches whereas green (master) represents the active branch.

The branches in red are the remote branches i.e.

  remotes/origin/br-tst1

  remotes/origin/br-tst3

  remotes/origin/master

How to show remote branches only?

The command below shows how to list only remote branches for the set repository:

$ git branch -r

The outcome:

Git list branches remote

The output shows only the branches in red that are remote branches.

List only local branches example

Again, using the branch command without any option lists the local branches only. Have a look:

$ git branch

The result:

show branches local

Only the local branches are listed in white with the master in green (which is the active branch).

Using Git grep command for local branches examples

For searching any committed tree, working directory, etc. you may use the grep command of Git.

The grep command is a big topic, however, in our context of showing branches, the command below shows how you may use it:

$ git branch -a | grep -v ‘remotes’

The result is:

show grep remote

You can see all local branches in the above graphic – without the active branch green color.

And if you want to get only remote branches then remove the -v in the above command:

$ git branch -a | grep ‘remotes’

You may learn more about the grep here.

See when another branch is active

In this example, I have used the checkout command to make the br-tst1 branch active.

This is followed by using the command for listing all branches and see the output:

show branches active

You can see, the br-tst1 is green now.

Using show-branch command example

In this example, I used the show-branch command to see branches and commits made. See the command and its output:

$ git show-branch

show branches

For listing the remote tracking branches, use the -r or –remotes option with the show-branch command.

For example:

$ git show-branch -r

The example output with our created branches in the above section:

show branches remote

Similarly, for seeing all branches/commits in remote and local repos, use the –a or –all option:

$ git show-branch –all
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!