How to Create a New Branch in Git

December 30, 2019


Git is an open-source version-control system for tracking changes during software development. 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 4 options to create a new branch in Git.

create new branch git tutorial


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

Create Git Repository Locally

To create a new Git repository, enter the following command in your terminal:

mkdir git-app

cd git-app

git init

This creates and initializes a new Git repository in the git-app directory. Create a new markdown file and add a line of text:

echo This is a line of text >

Check the status of the file:

git status

You’ll see the file is untracked.

Output confirms that the git file is untracked

Track it and create the first commit by entering the following:

git add

git commit -m "First Commit"
Confirmation of first git commit

Note: If this is the first time you’ve run Git, the operation may fail. Use the commands listed on the screen (Git config) to set your global email and username, then try again.

Create a New Git Branch

There are many ways you can 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>

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>

This will create a new branch from your current branch. It will also automatically switch you 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>

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

Commit is a command that saves changes you’ve made in the code. A project may have multiple commits as it’s revised and improved. Create a set of commits like in the sample below:

echo New line of text >>

git commit -a -m "New line added"

echo Second line of text >>

git commit -a -m "Second new line"

echo Third new line >>

git commit -a -m "Third new line"

echo Last new line >>

git commit -a -m "Last new line"

Note: If you receive an error “Nothing added to commit but untracked files present”, enter:

git add

Find the hash key for a specific commit:

git log

The log contains the hash key.

Hash key displayed on screen.

Create a branch from an older commit:

git branch <new_branch_name> 89198

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

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

This 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. To switch to the new branch, enter the following:

git checkout <new_branch_name>

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. Git checkout tags are used for production release versions of the software.

Create a tag in the test project:

git tag -a v1.0 -m "Version 1.0"

In a normal project, you would continue working on the software for the next release.

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

git branch <new_branch_from_tag> v1.0

To switch to this branch:

git checkout <new_branch_from_tag>

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 for one of your commits, then enter:

git checkout d1d307

Replace d1d07 with the actual hash value from your system. You’ll get a message that says:

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 this commit. Any changes will be lost, unless you save them.

To save your progress, stage it 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>

How to Delete a Git Branch

To delete a git branch use the command:

git checkout master

git branch -d <branch_name>

The output confirms that “first-branch” has been deleted.

System confirms that the git branch has been deleted


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?
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
April 10, 2019

Git is a widely-used software package in Linux. Its main purpose is to track changes in software code during...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.