Git Essentials(Second Edition)
上QQ阅读APP看书,第一时间看更新

Tags are fixed labels

Tags are labels you can pin to a commit, but unlike branches, they will stay there.

Creating a tag is simple: you only need the git tag command, followed by a tag name; we can create one in the tip commit of bug branch to give it a try; check out the bug branch:

[1] ~/grocery (master)
$ git checkout bug
Switched to branch 'bug'

Then use the git tag command followed by the funny bugTag name:

[2] ~/grocery (bug)
$ git tag bugTag

Let's see what git log says:

[3] ~/grocery (bug)
$ git log --oneline --graph --decorate --all
* 07b1858 (HEAD -> bug, tag: bugTag) Bug eats all the fruits!
| * a8c6219 (melons) Add a watermelon
| * ef6c382 (berries) Add a blackberry
| * 0e8b5cf (master) Add an orange
|/

* e4a5e7b Add an apple * a57d783 Add a banana to the shopping list

As you can see in the log, now on the tip of the bug branch there is even a tag named bugTag.

If you do a commit in this branch, you will see the bugTag will remain at its place; add a new line to the same old shopping list file:

[4] ~/grocery (bug)
$ echo "another bug" >> shoppingList.txt

Perform a commit:

[5] ~/grocery (bug)
$ git commit -am "Another bug!"
[bug 5d605c6] Another bug!
 1 file changed, 1 insertion(+)

Then look at the current situation:

[6] ~/grocery (bug)
$ git log --oneline --graph --decorate --all
* 5d605c6 (HEAD -> bug) Another bug!
* 07b1858 (tag: bugTag) Bug eats all the fruits!
| * a8c6219 (melons) Add a watermelon
| * ef6c382 (berries) Add a blackberry
| * 0e8b5cf (master) Add an orange
|/
* e4a5e7b Add an apple
* a57d783 Add a banana to the shopping list

That's exactly what we predict.

Tags are useful to give a particular meaning to some particular commits; for instance, as a developer, you maybe want to tag every release of your software: in that case, this is all you need to know to do that job.

Even tags are references, and they are stored, as branches, as simple text files in the tags subfolder within the .git folder; take a look under the .git/refs/tags folder, you will see a bugTag file; look at the content:

[7] ~/grocery (bug)
$ cat .git/refs/tags/bugTag
07b18581801f9c2c08c25cad3b43aeee7420ffdd

As you maybe have already predicted, it contains the hash of the commit it refers to.

To delete a tag, you have to simply append the -d option: git tag -d <tag name>.

As you can't move a tag, if you need to move it you have to delete the previous tag and then create a new one with the same name that points to the commit you want; you can create a tag that points a commit wherever you want, appending the hash of the commit as an argument, for example, git tag myTag 07b1858.