在Git中打标签非常简单,首先,切换到需要打标签的分支上,然后,敲命令git tag即可:
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git tag v1.0
可以用git tag查看所有标签
$ git tag
v1.0
默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,比如,现在已经是周五了,但应该在周一打的标签没有打,怎么办?
方法是找到历史提交的commit id,然后打上就可以了:
git log --pretty=oneline --abbrev-commit
6050685 (HEAD -> master, tag: v1.0, origin/master) merge with dev
d38ddc6 dev is right
$ git tag v0.9 d38ddc6
$ git log --pretty=oneline --abbrev-commit
6050685 (HEAD -> master, tag: v1.0, origin/master) merge with dev
d38ddc6 (tag: v0.9) dev is right
注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show 查看标签信息:
$ git show v0.9
commit d38ddc6460db71e5a6dcccb4710c3f54f7db81ac (tag: v0.9)
Author: 2470231941@qq.com <2470231941@qq.com>
Date: Tue Jan 30 22:21:04 2018 +0800
dev is right
diff --git a/readme b/readme
index 6a3654d..97c1668 100644
--- a/readme
+++ b/readme
@@ -4,3 +4,4 @@ Git has a mutable index called stage.
Git tracks changes of files.
Creating a new branch is quick AND simple.
new line.
+new line new line.
还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字:
$ git tag -a v0.1 -m "version 0.1" e2d1008
$ git show v0.1
tag v0.1
Tagger: 2470231941@qq.com <2470231941@qq.com>
Date: Thu Feb 1 19:12:29 2018 +0800
version 0.1
commit e2d10085ce4748d162d7cbfade465dfb8ecb1306 (tag: v0.1)
Merge: d50594a ce34493
Author: *********@qq.com <***********@qq.com>
Date: Tue Jan 30 22:06:52 2018 +0800
merged bug fix 101
下面的PGP签名,全部复制教程了。。。。。
还可以通过-s用私钥签名一个标签:
$ git tag -s v0.2 -m "signed version 0.2 released" fec145a
签名采用PGP签名,因此,必须首先安装gpg(GnuPG),如果没有找到gpg,或者没有gpg密钥对,就会报错:
gpg: signing failed: secret key not available
error: gpg failed to sign the data
error: unable to sign the tag
如果报错,请参考GnuPG帮助文档配置Key。
用命令git show 可以看到PGP签名信息:
$ git show v0.2
tag v0.2
Tagger: Michael Liao <askxuefeng@gmail.com>
Date: Mon Aug 26 07:28:33 2013 +0800
signed version 0.2 released
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (Darwin)
iQEcBAABAgAGBQJSGpMhAAoJEPUxHyDAhBpT4QQIAKeHfR3bo...
-----END PGP SIGNATURE-----
commit fec145accd63cdc9ed95a2f557ea0658a2a6537f
Author: Michael Liao <askxuefeng@gmail.com>
Date: Thu Aug 22 10:37:30 2013 +0800
branch test
用PGP签名的标签是不可伪造的,因为可以验证PGP签名。验证签名的方法比较复杂,这里就不介绍了。
小结
- 命令git tag 用于新建一个标签,默认为HEAD,也可以指定一个commit id;
- git tag -a -m “blablabla…”可以指定标签信息;
- git tag -s -m “blablabla…”可以用PGP签名标签;
- 命令git tag可以查看所有标签。