Table of Contents
This tip is about the how To Create a Git Branch. So read this free guide, How To Create a Git Branch step by step. If you have query related to same article you may contact us.
How To Create a Git Branch – Guide
Git makes creating and managing branches a lot easier. In fact, the power and flexibility of its branching model is one of Git’s biggest advantages! There are a few different use cases when branching in Git. Let’s look at each of them in turn.
Create Git branch using checkout
The easiest way to create a Git branch is to use the “git checkout” command with the “-b” option for a new branch. Then you just need to specify the name of the branch you want to create.
$ git checkout -b
As an example, let’s say you want to create a new Git branch of the master branch called “feature” To achieve this you will run the command “git checkout” with the option “-b” and add “feature” as the branch name.
$ git checkout -b featureSwitched to new branch ‘feature’
As you can see, using the “git checkout” command, you are creating a new branch and switching to this new branch automatically. But what if you want to create a Git branch without switching to the new branch automatically?
Create Git branch without switching
To create a new Git branch, without switching to this new branch, you must use the command “git branch” and specify the name of the Git branch to be created.
$ git branch
Later, you can switch to your new Git branch using the “git checkout” function.
$ git checkout
Going back to our previous example, let’s say you want to create a branch called “feature”.
$ git branch feature
You can inspect existing branches by running the “git branch” command with the “-a” option for all branches.
$ git branch -a
Awesome, you’ve successfully created a new Git branch and switched to it using the checkout command.
Create Git branch from commit
In the last few sections, we’ve seen How to create a new Git branch from the current branch’s HEAD commit. In some cases, you want to create a Git branch from a specific commit in your Git history.
To create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit from which to create your branch.
$ git checkout -b
Alternatively, you can use the “git branch” command with the branch name and SHA commit for the new branch.
$ git branch
Going back to our previous example, let’s say you want to create a Git branch from a specific commit in your Git history. To get SHA commits from your history, you should use “git log” with the “–oneline” option.
$ git log –oneline –graph
* 9127753 (HEAD -> master) Commit 3* f2fcb99 Commit 2* cab6e1b (origin/master) master: initial commit
To create a new Git branch from the second commit (f2fcb99), you must run the following command
$ git checkout -b feature f2fcb99Switched to a new branch called ‘feature’
Using the “git log” command, you can verify that your branch was created from the second commit in your history.
$ git log –oneline –graph
* f2fcb99 (HEAD -> feature) Commit 2* cab6e1b (source/master) master: initial commit
Awesome, you’ve successfully created a new Git branch from a specific commit!
Create Git branch from tag
in the previous tutorials, we have seen that Git tags are quite useful: they can be used as reference points in your development. As a consequence, it can be quite useful to create Git branches from existing tags.
To create a new Git branch from a tag, use the “git checkout” command with the “-b” option and specify the branch name as well as the tag name for your new branch.
$ git checkout -b
Alternatively, if you don’t want to switch to your new branch, you can use “git branch” with the branch name and the tag name.
$ git branch
Going back to our previous example, let’s say you want to create a new Git branch from a tag called “v1.0” in your history. To list your existing tags, you can use the “git tag” command. Alternatively, you can use the “git log” command to identify a tag associated with a commit.
$ git tagv1.0
Now that you’ve identified your tag, you can create a new branch from it using the “git checkout” command.
$ git checkout -b feature v1.0
You can then inspect your Git history to make sure your new branch was actually created from the tag. Alternatively, you could have used the “git branch” to create this branch.
$ git branch feature v1.0
Final note
I hope you like the guide How To Create a Git Branch. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.