How to Create Branch From Another Branch in Git
This tutorial teaches how to create a branch from another branch in Git.
Git, a distributed version control system, is an apt tool for versioning in a collaborative development environment. In Git, we create repositories, and in the repositories, we create branches to track various development efforts.
We often create a new branch from the mainline for fixing bugs or developing a new feature. After completing the task, we typically merge this branch back to the mainline branch for a release.
Git provides us the capability to create a branch from another existing branch. Also, we can merge the branches using Git commands.
Use the git checkout
Command to Create Branch From Another Branch in Git
Git, a distributed version control system, is a useful tool for tracking changes to the project repository.
We have multiple team members or teams using the same project repository for work in a collaborative development environment. Various team members or teams may create different branches from an existing branch to work on that branch.
Suppose we have a mainline branch named main
in our project repository. A bug fix team would create a new branch named bugfixes
on top of that branch. Another team or team member would create a branch feature
for developing a new feature.
Once the team or team members are satisfied with the changes done in the new branch, the new branch is often merged back into the mainline branch.
Suppose we want to create a branch feature
from the mainline branch main
for developing a feature. We can use the git checkout
command for that.
The syntax to create a new branch of the existing branch is below.
git checkout -b <new-branch> <existing-branch>
In our case, we will execute the command as follows.
$ git checkout -b feature main
Switched to a new branch 'feature'
Thus, we have created a new branch feature
off the existing branch main
. The option -b
to the command git checkout
causes the new branch to be created. Also, it causes the new branch to be checked out.
Now we would develop the new feature and make our changes in the new branch, namely feature
.
After completing the feature development and committing the changes to the new branch feature
, we would like to merge this branch with the mainline branch main
.
We can use the git merge
command to merge the feature
branch with the mainline branch main
.
First, we would switch to the mainline branch main
using the git checkout
command.
$ git checkout main
Switched to branch 'main'
We will use the git merge
command to merge the feature
branch with the main
branch.
We would execute the command as follows.
$ git merge --no-ff feature
Updating ea1b23a..05e9201
(Summary of changes)
The --no-ff
option to the command git merge
always enforces to create a new commit object, even if a fast-forward merge is performed. It causes to create a merge commit in all cases, even when the merge could be resolved as a fast-forward.
It’s useful, as when we browse the Git history, we would see information about the existence of the feature
branch. It also causes to group together all the commits of the feature
branch.
Also, when we desire to revert the merge of the feature
branch into the main
branch, it is easier because of the presence of the merge commit.
In the case of the fast-forward merge, no merge commit is created. Only the update of the branch pointer occurs to match the merged branch.
Finally, we need to push the changes to the remote repository.
$ git push origin main
$ git push origin feature
Thus, we have shown how to create a branch from another branch in Git.