How to create a new branch from a tag?

GitGit BranchGit Tag

Git Problem Overview


I'd like to create a new master branch from an existing tag. Say I have a tag v1.0. How to create a new branch from this tag?

Git Solutions


Solution 1 - Git

Wow, that was easier than I thought:

git checkout -b newbranch v1.0

Solution 2 - Git

If you simply want to create a new branch without immediately changing to it, you could do the following:

git branch newbranch v1.0

Solution 3 - Git

I used the following steps to create a new hot fix branch from a Tag.

Syntax

git checkout -b <New Branch Name> <TAG Name>

Steps to do it.

  1. git checkout -b NewBranchName v1.0
  2. Make changes to pom / release versions
  3. Stage changes
  4. git commit -m "Update pom versions for Hotfix branch"
  5. Finally push your newly created branch to remote repository.
git push -u origin NewBranchName

I hope this would help.

Solution 4 - Git

I have resolve the problem as below

  1. Get the tag from your branch

  2. Write below command

    Example: git branch git branch hotfix_4.4.3 v4.4.3 git checkout hotfix_4.4.3

or you can do with other command

git checkout -b <Hotfix branch> <TAG>
-b stands for creating new branch to local 

once you ready with your hotfix branch, It's time to move that branch to github, you can do so by writing below command

git push --set-upstream origin hotfix_4.4.3

Solution 5 - Git

The situation becomes a little bit problematic if we want to create a branch from a tag with the same name.

In this, and in similar scenarios, the important thing is to know: branches and tags are actually single-line text files in .git/refs directory, and we can reference them explicitly using their pathes below .git. Branches are called here "heads", to make our life more simple.

Thus, refs/heads/master is the real, explicit name of the master branch. And refs/tags/cica is the exact name of the tag named cica.

The correct command to create a branch named cica from the tag named cica is:

git branch cica refs/tags/cica

Solution 6 - Git

An exemple of the only solution that works for me in the simple usecase where I am on a fork and I want to checkout a new branch from a tag that is on the main project repository ( here upstream )

git fetch upstream --tags

Give me

From https://github.com/keycloak/keycloak
   90b29b0e31..0ba9055d28  stage      -> upstream/stage
 * [new tag]    11.0.0     -> 11.0.0

Then I can create a new branch from this tag and checkout on it

git checkout -b tags/<name> <newbranch>

git checkout tags/11.0.0 -b v11.0.0

Solution 7 - Git

My branch list (only master now)

branch list

My tag list (have three tags)

tag list

Switch to new branch feature/codec from opus_codec tag

git checkout -b feature/codec opus_codec

switch to branch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - GitAndrewView Answer on Stackoverflow
Solution 2 - GitThamaraikaniView Answer on Stackoverflow
Solution 3 - GitTarun KumarView Answer on Stackoverflow
Solution 4 - GitKirtikumar A.View Answer on Stackoverflow
Solution 5 - GitpeterhView Answer on Stackoverflow
Solution 6 - GitamdevView Answer on Stackoverflow
Solution 7 - GiterangaView Answer on Stackoverflow