Git branches are used to create clones of the specified code base and work easily by making changes. In order to work with branches, it should be created in Git. Branches can be created in git by using the git branch
command with different options for different git branch creation.
List Branches
Before creating a new branch it is very useful to list branches. The git branch
command can be used to list currently existing branches.
$ git branch
Create New Branch From Current Active Branch
A new branch can be created by specifying the new branch name to the “git branch” command. The syntax is like below.
git branch BRANCH_NAME
In the following example, we create a new git branch with the name of testing
.
$ git branch testing
The branch named “testing” is cloned and created from the current active branch which can be displayed by listing branched escribed above.
Create New Branch From Another Branch
By default, the current active branch is used to create a new branch but alternatively, we can specify different branches to clone and create a new branch. The syntax of creating a new branch from another branch.
git branch NEW_BRANCH_NAME BASE_BRANCH
In the following example, we create a new branch that is based on the “master” branch.
$ git branch testing master