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. 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. 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. Later, you can switch to your new Git branch using the “git checkout” function. Going back to our previous example, let’s say you want to create a branch called “feature”. You can inspect existing branches by running the “git branch” command with the “-a” option for all branches. 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. Alternatively, you can use the “git branch” command with the branch name and SHA commit for the new 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. To create a new Git branch from the second commit (f2fcb99), you must run the following command

  • 9127753 (HEAD -> master) Commit 3* f2fcb99 Commit 2* cab6e1b (origin/master) master: initial commit Using the “git log” command, you can verify that your branch was created from the second commit in your history. Awesome, you’ve successfully created a new Git branch from a specific commit!
  • f2fcb99 (HEAD -> feature) Commit 2* cab6e1b (source/master) master: initial 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. 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. 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. Now that you’ve identified your tag, you can create a new branch from it using the “git checkout” command. 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.

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.