官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 保靖县| 泰顺县| 修武县| 临武县| 宁阳县| 循化| 乌鲁木齐县| 米脂县| 凌海市| 日喀则市| 灵石县| 博罗县| 土默特左旗| 唐山市| 商城县| 德保县| 铜川市| 镇雄县| 鸡泽县| 平陆县| 沙雅县| 乌拉特中旗| 明光市| 安康市| 馆陶县| 贵州省| 德阳市| 崇州市| 延津县| 大同市| 堆龙德庆县| 汉中市| 泽库县| 华坪县| 武义县| 高雄市| 安阳市| 垣曲县| 辛集市| 瑞昌市| 砚山县|