菜鸟笔记
提升您的技术认知

git 标签

 Git 标签

标签也是版本库的一个快照。Git 的标签虽然是版本库的快照,但其实它就是指向某个commit 的指针

标签就是指向针对哪次commit打标签,标签指向某一次commit

如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。

 

标签有什么用?

前面回滚使用的是一串commit id 字符串,又长又难记

 

 

比如说,我们想为我们的 项目发布一个"1.0"版本。 我们可以用 git tag -a v1.0 命令给新一次提交打上(HEAD)"v1.0"的标签。

-a 选项意为"创建一个带注解的标签"。

不用 -a 选项也可以执行的,但它不会记录这标签是啥时候打的,谁打的,也不会让你添加个标签的注解。 我推荐一直创建带注解的标签

 

创建标签

 

针对某一次commit 打一个标签 例如v1.0

标签没有指定哪个commit 默认会指定当前最后一次提交的commit

 

[root@ci-node1 git_test]# git tag -a v1.0

 

当你执行 git tag -a 命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样

执行这条命令,会出现要输入文本框,对这次标签做一次注释, 最后wq保存

 

 

 

#
# Write a message for tag:
#   v1.0
# Lines starting with '#' will be ignored.
This is my v1.0 version
                                                                                             
:wq

现在,注意当我们执行 git tag 时,我们可以看到我们的标签了

打的标签针对当前这次提交做的标签

[root@ci-node1 git_test]# git tag
v1.0

 

针对某一个commit 打标签方法:

[root@ci-node1 git_test]# git log --oneline --decorate
cc7da0e (HEAD -> master, tag: v1.0) merge testing to master both modify a
1114bde modify a on testing branch
7fd08f1 modify a on master branch
3b06f44 Merge branch 'testing'
241209e commit master on branch master
9892af9 commit test on branch testing
ac31205 commit a
353666d a
af5856a modify a
52e8981 rename a.txt to a
cc8bd80 rename a to a.txt
73d7230 commit a

[root@ci-node1 git_test]# git tag -a v2.0 af5856a

编辑内容 保存

#
# Write a message for tag:
#   v2.0
# Lines starting with '#' will be ignored.
This is my v2.0 version.
                                                                                                    
:wq

再执行git tag 有两个标签

[root@ci-node1 git_test]# git tag
v1.0
v2.0

 

查看当前所有的标签

[root@ci-node1 git_test]# git tag
v1.0
v2.0

 

查看标签详细信息

我们可以使用 git show v1.0 来查看标签的内容

 

[root@ci-node1 git_test]# git show v1.0

 

 删除标签

[root@ci-node1 git_test]# git tag -d v2.0
Deleted tag 'v2.0' (was e143256)

[root@ci-node1 git_test]# git tag
v1.0

 

创建带有说明的标签,-a 指定标签名字,-m 指定说明文字

[root@ci-node1 git_test]# git tag -a v2.0 -m "version 2.0 release is test"

 

通过标签去标识某一次commit,简化commit id ,不用再去找commit id