How to Create a Branch From a Tag in Git

John Wachira Feb 02, 2024
How to Create a Branch From a Tag in Git

This article illustrates how we can create a new branch based on a tag in Git. If you are a regular Git user, you must know the purpose of Git tags.

These tags are simply indicators that point to a meaningful Git commit. The question is: how do you create a branch based on a Git tag?

Create a Branch From a Tag in Git

We can see the commits with tags if we run the git log command. Here is our commit history:

Commit History

Let’s say we wanted to create a new branch based on the tag v1.o.5 at the Release v1.0.5 - Bump Codebase Version commit. How would we go about it?

It is pretty simple. We will use the git branch command as illustrated below:

$ git checkout -b Tag-Branch v1.0.5

This command will create a new branch called Tag-Branch and carry all the commits up to the tag, including the one at the tag. Let’s check if this is the case.

New Branch

We can see that Git has created a new branch. The commits that came after our tag was deleted when creating the branch.

Alternatively, we can opt to reset the HEAD to the tag and create a new branch based on the head. It is not a clean way since you will remove the commits from your branch.

You can run the command below:

$ git reset --hard <tag>
$ git checkout -b newbranch

In conclusion, Git allows us to create new local branches based on any tag in our repository. Using the git reset --hard option is not always the best option.

The git branch -b new-branch <tag> is a cleaner way of creating branches based on tags.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Branch