Whenever you start developing a new feature you must start with creating a new branch git. This way you separate your feature changes from other branches.
A new branch can be created with the ‘git checkout’ command with the ‘-b’ option and a new branch name as arguments. This will create a new branch from your current branch and also switch the branch to the new branch.
$ git checkout -b <new-branch>
But if you don't want to create a new branch from the current branch then you can also pass the base branch as an argument.
$ git checkout -b <new-branch> <base-branch>
Once the feature is completed, this new branch can be merged back into the other branch.
Example of creating a GIT branch
Let's take an example, you want to create a new feature branch 'feature-2' from the master branch.
1. Checkout master branch (source branch)
First, make sure your current git branch is the master branch. You can check this by running "git branch" or "git branch --show-current" commands
$ git branch feature-1 * master
If not, then make sure to checkout the master branch.
$ git checkout master
2. Run the "git checkout" command with "-b" and feature the branch name
Now you can run the GIT checkout command to create a new feature branch "feature-2"
$ git checkout -b feature-2 Switched to a new branch 'feature-2'
Once run successfully, the new branch will be created and your current branch will be switched to this new branch.
3. Confirm the new branch
You can verify your new branch by running the ‘git branch’ command.
$ git branch feature-1 * feature-2 master
In this tutorial, we learned to create a new branch in GIT with the "git checkout" command. This can also be achieved with the "git branch" command.
A GIT branch is not the only source for creating a new feature branch. A New GIT branch can also be created from commit SHA, a tag, or a remote branch.
Add new comment