Home > Git > How to Create a New Git Branch?

How to Create a New Git Branch?

When you are working in a team on a big project and use are using Git for a version controlling system to track all file changes and keep them secure.

So, using Git, you want to create a new git branch when you want to add a new feature or fix the bugs on the project.

You can create a separate new branch from the main branch and then after all the fixes or new feature implementation, you can merge them later. When you merged all changes successfully then you can delete that git branch.

If you have all the data in the main master branch then you can create a new separate branch and can develop a new feature in that branch. After all is done with the feature, you can merge your newly-created branch with the master branch.

So let’s start. In this tutorial, we will see how we can create a new git branch and use it to develop new changes and bug fixing.

Create a New Git Branch Using Checkout

This is the simple and easiest way to create a new git branch using the git checkout command. You just have to add the -b flag option with this command in order to create a new branch.

After the -b flag option, specify the branch name that you want to create. See the following example command.

Syntax:

Let’s take an example that you want to add a new feature to a project and for this feature, you want to create a new branch “new-feature” from the master branch.

To create a “new-feature” branch, you have to run the git checkout command with the -b flag and specify the branch name.

When you run the above command it will create a new branch and switch to it automatically.

There is also a way to create a new branch without switching automatically to a newly created branch. Let’s see how you can achieve this.

Create a New Git Branch Without Switching

To create a new git branch without switching to it, you have to run the git branch command without and flag option and then specify the branch name.

Syntax:

Let’s create a new branch name called “new-feature” using the above git command.

The above command will create a new branch and it will not switch to the newly created branch. You can switch later using the git checkout command.

Create a New Git Branch From Commit

In the above examples, we have seen how to create a git branch from the master HEAD commit but what if you want to create a new git branch from the commit history? Yes, you can also do this using the git checkout command and other more options.

To create a new git branch from a specific commit, then you have to run the git checkout command with the -b flag option and then specify the branch name and commit SHA id.

The above command will create a new branch from the specified commit and it will automatically switch to the newly created branch.

If you want this same scenario but without switching then you can use the git branch command.

Create a New Git Branch From Tag

As the same above example, we can also create a new branch from the git tag. Tag as reference points in your development environment.

So, we will use the same above git checkout command and git branch command along with specifying the flag option and branch name.

Use the following command if you don’t want to switch to the newly created branch.

Bonus of Tutorial

View Your Git Branch List

As per the tutorial, you have seen how to create a new git branch. In case you want to see your branch has been created successfully or not then you can use the git branch command with -a flag option.

It will print the list of branches. So you can use this command before and after creating a new branch then compare both cases to check our branch is created.

View Commit SHA ID

As we have created a new branch above from the commit history using the commit SHA id. So if you don’t know how to see commit SHA id then use the git log following command with --online and --graph flags.

It will print the git log history then you can get the commit SHA id and use it in the creation of a new branch from commit.

View the Git Tag Point

In order to see the existing git tag then you can use the git tag command. It will print all existing git tags and you can use them in the case of creating a new branch from the git tag.

Create and Checkout Git Branch at Same Time

As we see in the first example, how to create a branch and checkout (switch) at same time. Then use the git checkout command with the -b flag.

This command will create a new git branch and checkout immediately at the same time.

Conclusion

So, in this tutorial, you learned about how to create a new branch using the checkout command, using the commit history, using the tag point.

For referencing these above you also learned how to see all the branches and get the commit SHA id, and get the tags to point to use them while creating a new branch.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment