Git branches provides very useful way to work with a project in multi developer mode. Every developer can use different or his branch to implement new features and then merge them into a main branch. In this tutorial we will learn how to list and print branch information.These branches can be local or remote too.
Print/Show Current Branch
First step to get or list branch is printing or showing current branch. We will use just
git branch command which will list all branches. But current branch will be denoted with the asterisk
*. In this example
master is our current or working branch.
$ git branch
List Local Branches
Actually we have all ready listed local branches in previous example but in order to express the current working branch I do not provide this information. So we can list all local branches with the
git branch command. In most cases there will be more than one branch.
$ git branch
List All Remote Branches
Up to now we have worked with the local branches but also remote branches are important part of the distributed software development because other developers will push their branches and this will be remote branch for us. We can list all remote branches with the
git branch -r command.
-r means remote for branch.
$ git branch -r
List All Branches (Local and Remote)
We can also list all branches those resides on local or remote. We will use
-a option to the
git branch command. First the local branches will be listed and then the remote branches will be listed.
$ git branch -a
List Matched Branches
We can also list branches with given name pattern. We will use
--list option and provide the string we want to match. In this example we will list only branches those names starts with
test. We will also use wildcard
* for match any character after
$ git branch --list 'test*'
Change/Switch Current Branch
We generally list local or remote branches in order to change current working or default branch. We can change to the local or remote branch by providing the branch name. In this example we will change to the branch named
$ git checkout ismail