创建标签

    在Git中打标签非常简单,首先,切换到需要打标签的分支上:

    $ git branch

    * dev

    master

    $ git checkout master

    Switched to branch 'master'

    然后,敲命令 git tag <name> 就可以打一个新标签:

    $ git tag v1.0

    可以用命令 git tag 查看所有标签:

    $ git tag

    v1.0

    默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,比如,现在已经是周五了,但应该在周一打的标签没有打,怎么办?

    方法是找到历史提交的commit id,然后打上就可以了:

    $ git log —pretty=oneline —abbrev-commit

    6a5819e merged bug fix 101

    cc17032 fix bug 101

    7825a50 merge with no-ff

    6224937 add merge

    59bc1cb conflict fixed

    400b400 & simple

    75a857c AND simple

    fec145a branch test

    d17efd8 remove test.txt



    比方说要对 add merge 这次提交打标签,它对应的commit id是 6224937 ,敲入命令:

    $ git tag v0.9 6224937

    再用命令 git tag 查看标签:

    $ git tag

    v0.9

    v1.0

    注意,标签不是按时间顺序列出,而是按字母排序的。可以用 git show <tagname> 查看标签信息:

    $ git show v0.9

    commit 622493706ab447b6bb37e4e2a2f276a20fed2ab4

    Author: Michael Liao <askxuefeng@gmail.com>

    Date: Thu Aug 22 11:22:08 2013 +0800



    add merge



    可以看到, v0.9 确实打在 add merge 这次提交上。

    还可以创建带有说明的标签,用 -a 指定标签名, -m 指定说明文字:

    $ git tag -a v0.1 -m "version 0.1 released" 3628164

    用命令 git show &lt;tagname&gt; 可以看到说明文字:

    $ git show v0.1

    tag v0.1

    Tagger: Michael Liao <askxuefeng@gmail.com>

    Date: Mon Aug 26 07:28:11 2013 +0800



    version 0.1 released



    commit 3628164fb26d48395383f8f31179f24e0882e1e0

    Author: Michael Liao <askxuefeng@gmail.com>

    Date: Tue Aug 20 15:11:49 2013 +0800



    append GPL

    还可以通过 -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 &lt;tagname&gt; 可以看到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签名。验证签名的方法比较复杂,这里就不介绍了。

    http://michaelliao.gitcafe.io/video/git-tags.mp4