3 Ways to List branches in Git (Local and Remote) – jQuery-AZ

The command to list

all branches in local and remote repositories is

: $ git branch -a

If you only need to list the remote branches of Git Bash, use this command

: $ git branch -r

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

$

git

show-branch

As we work with the Git version control system, we have to work with branches. Our repositories can contain multiple branches, and sometimes it’s hard to remember all of them, either in the local or remote repository.

Therefore, it becomes very useful to know how to list all branches in the repository which also helps to switch to the appropriate branch

.

In the next section, I’ll show you an example of a remote, local repository. I will create some branches in both repositories and then use the commands to list all the branches as well as the branches in the remote repository with screenshots only.

The example of showing branches in

Git

For our example, I have created some branches in local and remote repositories.

Learn how to create local/remote

repositories/

branch offices

The following commands are used to create

the local branches: $ git branch br-tst1 $ git branch bt-tst2 $ git branch br-tst3

This is followed by the creation

of remote branches: $ git push origin br-tst1

$ git push origin

br-tst3

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

Example of displaying

all branches To list all branches – in

local and remote repositories, run this command

in the terminal:

$ git branch -a

The result is shown in the following graph:

Git list branches all

The blank branches are the local branches, while the green (master) represents the active branch.

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

how to display only the remote branches

? The

following command shows how to enumerate only remote branches for the established repository

:

$ git branch -r

The result:

Git list branches remote

The result

shows only the branches in red that are remote branches.

Local branches only list example

Again

, using the branch command without any option lists only the local branches. Take a look

:

$ git

branch

The result:

show branches local

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

Using the Git grep command

for local branch examples

To search any compromised tree, working directory, etc., you can use the Git grep command. The grep command is an important topic, however, in our context of displaying branches, the following command shows how you can use it

:

$ git branch -a | grep -v

‘remote’

The result is:

show grep remote

You can see all local branches in the graph above, with no active branch green color.

And if you want to get only remote branches, remove the –v command in the previous one:

$ git branch -a | grep ‘remotes’

You can learn more about grep here

.

See when another branch is active

In this example, I used the checkout command to activate the br-tst1 branch. This is followed by using the command to list all branches and see the output:

show branches active

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

Using the show-branch command example In this example, I used the

show-branch command

to view the branches and commits made. See the command and its output:

$ git

show-branch

<img src

=”https://www.jquery-az.com/wp-content/uploads/2018/06/5.0_5-show-branches.png” alt=”show branches” />

To list 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 branches created in the previous section

:

<img src

=”https://www.jquery-az.com/wp-content/uploads/2018/06/5.0_6-show-branches-remote.png” alt=”show branches remote” />

Similarly, to view all branches/commits in remote and local repositories, use the -a or

-all option:

$ git show-branch -all