Git允许你对提交打标签,这可以用来创建版本。比如说,开始发布了v0.1,然后你添加了一些功能,版本是v0.2。可以使用标签指定哪个提交是v0.1,哪个提交是v0.2。
Git有两种类型的标签:
- 注释标签 比较详细,包含作者、消息等。
- 轻量级标签 简略,就像指向特定提交的简单指针一样。
创建标签
创建注释标记
要创建带注释的标记,请使用git tag -a
命令。
$ git tag -a <tag-name>
这将打开一个临时文件,用默认文本编辑器编辑标签消息。这类似于指定提交消息。要在命令行中指定消息,可用-m
选项。
$ git tag -a <tag-name> -m <tag-message>
例子
$ git tag -a v0.1 -m 'First version. Adds file 1'
要列出打过标签,执行git tag
$ git tag
v0.1
要查看特定标签的信息,可以使用git show
命令,
$ git show v0.1
tag v0.1
Tagger: kevinhwu <kevinhwu@qikegu.com>
Date: Fri Aug 23 21:25:54 2019 +0800
first version
commit ac4a30fd05334a9e5df462c28f028b2da1fd834d (HEAD -> master, tag: v0.1)
Merge: f78d09a 952114c
Author: kevinhwu <kevinhwu@qikegu.com>
Date: Fri Aug 23 14:51:05 2019 +0800
update test1.txt
创建轻量级标签
要添加轻量级标记,可使用git tag <tag name>
,但要避免使用-a
或-m
选项。
$ git tag v0.1-b
$ git tag
v0.1
v0.1-b
$ git show v0.1-b
commit ac4a30fd05334a9e5df462c28f028b2da1fd834d (HEAD -> master, tag: v0.1-b, tag: v0.1)
Merge: f78d09a 952114c
Author: kevinhwu <kevinhwu@qikegu.com>
Date: Fri Aug 23 14:51:05 2019 +0800
update test1.txt
对旧提交打标签
要对旧提交打标签,只需将提交散列的前几个字符,附加到创建标签命令后面即可。
$ git log --oneline
ac4a30f (HEAD -> master, tag: v0.1-b, tag: v0.1) update test1.txt
f78d09a update test1.txt
d574d8e add 'uvwxy' to test1.txt
952114c add text1.txt
ca0ad8e (origin/master, origin/HEAD) modified: test1.txt
454d25c add 'klmno' to test1.txt
c9f926d change 1st line of test1.txt: 'file name is test1.txt'
ad729df change 1st line of test1.txt: 'file type is txt'
724b9f7 add test2.txt
34c2089 add 'fghij' to test1.txt
3aa4060 add 'abcde' to test1.txt
...
$ git tag v0.2 f78d09a
$ git tag
v0.1
v0.1-b
v0.2
改变标签对应的提交
执行改变标签对应的提交,会报错。
$ git log --oneline
ac4a30f (HEAD -> master, tag: v0.1-b, tag: v0.1) update test1.txt
f78d09a update test1.txt
d574d8e add 'uvwxy' to test1.txt
952114c add text1.txt
ca0ad8e (origin/master, origin/HEAD) modified: test1.txt
454d25c add 'klmno' to test1.txt
c9f926d change 1st line of test1.txt: 'file name is test1.txt'
ad729df change 1st line of test1.txt: 'file type is txt'
724b9f7 add test2.txt
34c2089 add 'fghij' to test1.txt
3aa4060 add 'abcde' to test1.txt
...
$ $ git tag v0.2 d574d8e
fatal: tag 'v0.2' already exists
必须使用-f
开关强制更改标签指向的提交。
$ git tag v0.2 d574d8e -f
Updated tag 'v0.2' (was f78d09a)
将标签推送到远程仓库
要将所有标签推送到origin,可以使用git push --tags
命令,
$ git push <remote> --tags
举例
$ git push origin --tags
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 4 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1016 bytes | 254.00 KiB/s, done.
Total 9 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/kevinhwu/git-demo.git
* [new tag] v0.1 -> v0.1
* [new tag] v0.1-b -> v0.1-b
* [new tag] v0.2 -> v0.2
删除标签
删除标签可以使用git tag -d <tag-name>
,
$ git tag -d <tag-name>
$ git tag -d v0.2
Deleted tag 'v0.2' (was d574d8e)
$ git tag
v0.1
v0.1-b
但是,远程仓库上的标签仍然未删除。
$ git push origin --tags
Everything up-to-date
本地删除标签后,要在远程仓库上也删除,需要手动将标签引用(tag refs)推送到远程。
$ git push origin :refs/tags/v0.2
To git@github.com:nitstorm/gitexperiments.git
- [deleted] v0.2