2

I cloned a repository. In the repository there are several branches but there is also a tag. Now, I have been instructed to work with the version of that tag.

however I have to do some integration and make some more commits from that tag.

I don't know where in the structure of branches this tag is, so what I would like to do is to transform this tag into a branch (right now, after checkout to that tag I am into a "dettached state") so that later I can do commits merges etc.

Is there a way to do this?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

2 Answers2

3

You can just checkout the tag:

git checkout tag_name

then create a new branch from it, since tags are read only, and you only push the change to the branch (for bug fix or feature):

git checkout -b new_branch
ROOT
  • 11,363
  • 5
  • 30
  • 45
  • Thanks. I did that. One question though. How do I know how far that new branch is from ...say the master branch ? – KansaiRobot May 29 '20 at 04:08
  • you can run git diff against master branch or if you have `gitk` or `gitg` you can set the diff using these tools visually. – ROOT May 29 '20 at 04:10
  • 2
    @KansaiRobot `git log ..master` prints the commits which are on `master` and not on `` yet. – ElpieKay May 29 '20 at 06:51
3

A tag is a name for a commit. So is a branch name:

$ git rev-parse master
d2ecc46c0981fb829fdfb204604ed0a2798cbe07
$ git rev-parse v2.21.0^{commit}
8104ec994ea3849a968b4667d072fedd1e688642

Now, you might wonder what that ^{commit} is doing in there. That's because a tag is often also a name for an annotated tag object, which then names the commit:

$ git rev-parse v2.21.0
2bb64867dc05d9a8432488ddc1d22a194cee4d31
$ git cat-file -p 2bb64867dc05d9a8432488ddc1d22a194cee4d31
object 8104ec994ea3849a968b4667d072fedd1e688642
type commit
tag v2.21.0
tagger Junio C Hamano <gitster@pobox.com> 1551023739 -0800

Git 2.21
-----BEGIN PGP SIGNATURE-----
[snip]

If you want to start a branch at the commit selected by a tag, just do that:

git branch newbranch v2.21.0

You don't need the ^{commit} as git branch itself figures that out for you:

$ git branch newbranch v2.21.0
$ git rev-parse newbranch
8104ec994ea3849a968b4667d072fedd1e688642

What you probably should not do is remove the tag name and create a new branch name identifying the right commit, nor create a branch name that is the same as the tag name. Both of these are allowed but neither produces happy-making results.

In particular, once a tag has gotten around, every Git repository that has picked it up since then knows that it is a tag name and that it means that commit. Now you plan to change that fact? That's not nice to those other Git repositories.

Meanwhile, if you make a branch and tag name that are spelled the same, Git doesn't always resolve things the way you want. That is:

$ git branch v2.21.0 v2.21.0
$ git rev-parse v2.21.0
warning: refname 'v2.21.0' is ambiguous.
2bb64867dc05d9a8432488ddc1d22a194cee4d31

Sometimes, the name v2.21.0 will now mean the branch name. Sometimes it will mean the tag. Since branch names, by design, move from one commit (the old tip of the branch) to another (the new tip of the branch) as the branch grows, the two names will, in the future, resolve to two different commits.

Just use a different name. (And, as ROOT points out, you can create the branch after doing a checkout of the tag, by name; or you can do git checkout -b new-branch tag-name to do it all at once.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • After I create a branch in the tag, is there a way to check how far that is from the master branch? – KansaiRobot May 29 '20 at 04:09
  • 1
    You can do it even *before* you create the branch. (The branch isn't created "in the tag", it's created *pointing to* the same *commit*.) Define what you mean by "how far", though: see [Number of Commits between two Commitishes](https://stackoverflow.com/q/31997999/1256452) – torek May 29 '20 at 07:51