How to create a new branch from a tag?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Creating a branch from a tag in Git is straightforward once you remember what a tag actually is: a reference to a specific commit. A branch is just another reference, but unlike a tag, it is meant to move forward as you add commits. So creating a branch from a tag really means creating a movable reference that starts at the tag's commit.
The Basic Command
If the tag already exists locally, create the branch directly from it.
Older Git versions can use:
Both commands create a new branch named hotfix/v1.2.3 and point it at the commit referenced by tag v1.2.3.
Fetch Tags First if Needed
If the tag exists only on the remote, fetch tags before creating the branch.
You can inspect available tags with:
This avoids the common mistake of assuming the tag is present locally when it is not.
Why This Works
A tag identifies a fixed commit. Git does not care whether a new branch starts from a commit reached by a branch, by a tag, or by a raw commit hash.
That means these are conceptually similar:
In each case Git resolves the reference to a commit and creates a new branch starting there.
Use Cases for Branching From a Tag
This is common when you need to:
- create a hotfix from a released version
- backport a bug fix to an old release line
- investigate behavior from a specific shipped snapshot
- start maintenance work from a stable milestone
A tag is often the cleanest base for this because it points to the exact release commit rather than whatever main looks like today.
Push the New Branch to the Remote
Creating the branch locally is only the first step. If teammates or CI need it, push it.
The -u flag sets the upstream so later git push and git pull commands know which remote branch to use.
Annotated and Lightweight Tags Both Work
Git supports annotated tags and lightweight tags, but for branch creation that distinction usually does not matter. Both kinds of tag still resolve to a commit for this purpose.
The difference matters more for metadata, signing, and release documentation than for the actual branch-creation step.
Verify the Branch Base
If the branch is important, check that it really points where you expect.
This is especially useful in repositories where tag names are similar or where multiple maintenance lines exist.
Do Not Commit on Detached HEAD by Accident
A common alternative workflow is checking out the tag directly.
That puts you in detached HEAD state. It is fine for inspection, but not ideal for ongoing development because new commits are not attached to a branch name. If you intend to make changes, create the branch explicitly.
Common Pitfalls
- Forgetting to fetch tags before trying to branch from a remote tag.
- Checking out the tag directly and then committing in detached
HEADstate. - Assuming tags behave like branches and move over time.
- Creating the branch locally but forgetting to push it to the remote.
- Branching from the wrong similarly named tag without verifying the target commit.
Summary
- A tag is a fixed reference to a commit.
- Creating a branch from a tag means creating a movable branch reference at that commit.
- Use
git switch -c new-branch tag-nameorgit checkout -b new-branch tag-name. - Fetch tags first if the tag is not local.
- If you intend to develop from a tag, create a branch instead of staying on detached
HEAD.
Related reading
- How to create a remote Git repository from a local one?
- How to create file execute mode permissions in Git on Windows?
- How to deal with Git Could not read error
- How to delete a stash created with git stash create?
- how to delete all commit history in github?
- How to determine the URL that a local Git repository was originally cloned from
- How to diff a commit with its parent
- How to diff one file to an arbitrary version in Git?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.