Git is the best known distributed version control system at the moment. Sometimes you may require to list available branches within your git repository. In this post, we will see different ways to list available branches.

As we are aware GIT is distributed version control system, that said it has replicas of data locally and remotely repositories. This concept of remote and local applicable to branches as well. We can create local branches and do our work locally and once we are confident we can push those changes by creating a remote branch with the same name. This post is bit details on listing branches with examples for people who are new to git and experienced alike.

The listing of git branches has total three answers with the above explanation. Below are those three type of git branches.

Git list local branches
Git list only remote branches
Git list all branches

List all local git branches

git branch

Example:

surendra@linuxnix:~/code/sh/gittraining$ git branch
first_branch
* master

In the above output if you observe we have two branches locally available and master is my present branch(* indicates present branch) where I am located.

Note: We can execute git branch from any directory with in a repository.

List all remote git branches

git branch -r

-r is for listing any remote branches our git knows.

Example:

surendra@linuxnix:~/code/sh/gittraining$ git branch -r
origin/HEAD -> origin/master
origin/master

List all remote and local git branches

git branch -a

Example:

surendra@linuxnix:~/code/sh/gittraining$ git branch -a
first_branch
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master

Note: If you observe above output, the coloring indicates remote(orange) and local branches(green)

In our next post, we will see how to merge two branches. For all git related post click on this link.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.