How to Create a New Branch in Git

February 24, 2022

Introduction

Git is an open-source version-control system for tracking changes during the software development life cycle.

It’s mutually independent branching model makes it stand out. Branches can be based on previous versions of the software to maintain the integrity of current progress while working on a bug fix or new feature.

This guide will detail multiple options to create a new branch in Git.

create new branch git tutorial

Prerequisites

Note: This guide uses a Linux environment. Git is also available on Windows and macOS.

Create a New Git Branch

There are many ways to create a new Git branch. In most cases it comes down to whether you are creating a branch from the main branch or, for example, a new commit or tag.

One common method of creating a new branch is with the command:

git branch <new_branch_name>

This doesn’t automatically switch to that branch. To switch Git branches, enter the following command:

git checkout <new_branch_name>
git create and checkout new branch

Note: Instead of <new_branch_name> type the name for the new branch.

Create New Git Branch From Current Branch

The easiest and most popular way of creating a Git branch is:

git checkout -b <new_branch_name>
git checkout -b new branch

This creates a new branch from the current branch. It also automatically switches to the new branch.

Create New Git Branch From a Different Branch

To create a new branch from a different branch, run the following command:

git checkout -b <new_branch_name> <specific_different_branch>
git create branch from diferent branch terminal

Instead of <new_branch_name> type the name for the new branch, and instead of <specific_different_branch> type the name of the existing branch from which the new one shall be created.

Create a Branch from a Commit

A commit is a command that saves the changes made in the code. A project may have multiple commits as it's revised and improved.

Find the hash key for a specific commit:

git log

The log contains the hash key.

git log commit hashes

Create a branch from an older commit:

git branch <new_branch_name> 6009fc
git new branch from hash key

Note: 6009fc in the example above represents the hash. Replace this with the actual hash from your git log command.

No need to enter the whole hash key, just the first few characters. View the git log again, and you’ll see the new branch listed.

This method is especially helpful if you need to go back to a previous version of the software to fix a bug without removing any existing features.

Use git checkout to switch to the newly created branch.

Create a Branch from a Tag

A tag is a final, unchangeable version of a commit. Where a commit can be edited, tagged versions are usually permanent.

To create a branch from this tag, use the command:

git branch <new_branch_name> <tag_name>

To switch to this branch:

git checkout <new_branch_from_tag>
git new branch from tag

For more details, check our in-depth Git checkout tag guide.

Create a Branch Using Detached HEAD State

Detached HEAD state happens when you check out a commit that’s not formally part of a branch.

To test, use git log to get the hash of a commit, then enter:

git checkout 6009fc
git checkout detached head state

Replace 6009fc with the actual hash value from the system. The system prints the following output:

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

Just like the warning outlines, you can make changes based on the commit. Changes are lost if you don't save them.

To save any changes, stage it and then enter the following:

git commit -m "test_case"

git branch <test_case>

git checkout <test_case>

To add the changes into the master, use the following:

git checkout master

git merge <test_case>

Create a Branch from a Remote Branch

To create a new branch locally based on an existing remote branch, use the --track option:

git branch --track <new_branch> origin/<remote_branch>
git branch tracking remote

Alternatively, use the git checkout command to keep the original remote branch name:

git checkout --track origin/<remote_branch>

The git checkout command automatically creates the remote branch locally with the original name.

Create a Branch in a Remote Repository

Use the git push command to create a new branch in a remote repository based on a local branch:

git push -u origin <local_branch>

The command automatically creates the branch in a remote repository. The -u option ensures a tracking connection.

How to Delete a Git Branch

To delete a git branch use the command:

git checkout master

git branch -d <branch_name>
git branch -d delete branch output

The output confirms that the branch has been deleted.

Conclusion

You now know how to create branches in Git. Branches can be used to test-optional features before integrating them. Use small, short-term branches, so that the branch doesn’t stray too far from the central project.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How To Switch Branch on Git
February 3, 2020

Developers need to switch between branches frequently. Git branches allow you to work on your code, fix bugs...
Read more
How to Install and Use Git on Windows
January 8, 2020

Git tracks source code changes during the software development process. It can help coordinate work among...
Read more
How To Rename a Local and Remote Git Branch
January 6, 2020

Git is a version control system that helps you control the stages of software development. It uses named...
Read more
How To Install Git on Ubuntu 18.04/20.04
December 1, 2022

Git is a widely-used software package in Linux. Its main purpose is to track changes in software code during...
Read more