783

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?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Andrew
  • 227,796
  • 193
  • 515
  • 708

9 Answers9

1249

Wow, that was easier than I thought:

git checkout -b newbranch v1.0

If running the above command you get an error

warning: refname 'v1.0' is ambiguous.
fatal: ambiguous object name: 'v1.0'

this is because you also have a branch with name v1.0. In this case specify tag/ prefix:

git checkout -b newbranch tags/v1.0
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 63
    Correct. Note you also could have just set the master branch back to the point of the tag with `git reset --hard v1.0` – wadesworld Jun 09 '12 at 20:02
  • 11
    If this doesn't work because of " is not a valid commit" or a similar error (often when working on a shared repository), refer to https://stackoverflow.com/questions/35979642/what-is-git-tag-how-to-create-tags-how-to-checkout-git-remote-tags#35979751 – SalmonKiller Nov 09 '18 at 23:04
  • 24
    The suggestion from @wadesworld could work, but if anyone reads this and isn't 100% sure what it means to reset the master branch, *don't do that*. – Nathan Long Jan 22 '19 at 16:12
  • This is a better answer that I found here - https://stackoverflow.com/a/35979751/3145960 – Reaz Murshed Mar 11 '20 at 20:41
  • `git switch -C` should be the more "modern" way; see the [below answer](https://stackoverflow.com/a/73153620/3429115) – Rafs Jan 25 '23 at 16:41
145

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

git branch newbranch v1.0
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Thamaraikani
  • 1,451
  • 1
  • 9
  • 4
  • 7
    I think this should be the accepted answer as it does exactly what is required. The accepted answer does something else not stated in the question. – javi_swift Apr 30 '19 at 10:10
143

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Tarun Kumar
  • 2,918
  • 3
  • 15
  • 17
19

I have resolve the problem as below 1. Get the tag from your branch 2. Write below command

Example: git branch <Hotfix branch> <TAG>
    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
Kirtikumar A.
  • 4,140
  • 43
  • 43
9

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
peterh
  • 11,875
  • 18
  • 85
  • 108
9

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
amdev
  • 3,010
  • 3
  • 35
  • 47
9

At the time of writing this answer, the most up-to-date idiomatic git use is this command:

git switch -C branch tag
László Papp
  • 51,870
  • 39
  • 111
  • 135
3

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

eranga
  • 519
  • 7
  • 17
1

i was doing this like the other answers state with git checkout <tag> -b <new-branch-name> (and not the order of the tag vs branch being entered doesnt matter), even after doing a git fetch and was still getting a

fatal: 'my-tag' is not a commit and a branch 'temp-release-xxx' cannot be created from it

when i ran the more explicit git fetch --tags, then i was able to run the above command successfully

i thought git fetch would fetch all tags too, but sometimes you gotta nudge git i guess

heug
  • 982
  • 2
  • 11
  • 23