专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »编程综合 » Git教程( 5):Git标签 »正文

Git教程( 5):Git标签

来源: 发布时间:星期日, 2009年12月20日 浏览:0次 评论:0
  跟大多数VCS工具git也有在历史状态关键点“贴标签”功能--般人们用这个功能来标记发布点(例如'v1.0')这节课我们学习如何使用标签列表创建新标签以及在git中有哪些区别类别标签

  列出git中现有标签

  要想列出git中现有所有标签输入'git tag'命令运行即可:

$ git tag
v0.1
v1.3


  这个列表是按照字母表顺序给出其实排名先后跟重要程度没有直接联系

  当然你也可以按照特定表达式搜索某些标签假如在个git仓库中有超过240个标签而你只想得到1.4.2序列标签那么你可以:

$ git tag -l v1.4.2.*
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4


  创建标签

  在git中有两种最主要标签--轻量级标签(lightweight)和带注释标签(annotated)轻量级标签跟分枝不会改变--它就是针对某个特定提交指针然而带注释标签是git仓库中对象它是组校验和包含标签名、email、日期标签信息GPG签名和验证般情况下建议创建带注释标签这样就会保留这些信息但是如果你只是需要临时性标签或者某些原因你不想在标签中附带上面说这些信息lightweight标签更合适些

  带注释标签

  在git中创建带注释标签非常简单在运行'tag'命令时加上-a就可以了

$ git tag -a v1.4 -m 'version 1.4'
$ git tag
v0.1
v1.3
v1.4


  '-m'指明标签信息跟标签起存储如果你不使用-m指明标签信息git会自动启动文本编辑器让你输入

  可以使用 git show 命令查看相应标签版本信息并连同显示打标签时提交对象

$ git show v1.4
tag v1.4
Tagger: Scott Chacon <[email protected]>
Date:   Mon Feb 9 14:45:11 2009 -0800
my version 1.4
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon <[email protected]>
Date:   Sun Feb 8 19:02:46 2009 -0800
    Merge branch 'experiment'


  我们可以看到在提交对象信息上面列出了此标签提交者和提交时间以及相应标签信息

  有签名标签

  如果你有GPG私钥你也可以用GPG来给你标签签名把-a换成-s就可以了:

[master]$ git tag -s v1.5 -m 'my signed 1.5 tag' 
You need a passphrase to unlock the secret key for
user: "Scott Chacon <[email protected]>"
1024-bit DSA key, ID F721C45A, created 2009-02-09


  然后如果你对某个标签运行'git show'你就会看到你GPG前面附加上去了

[master]$ git show v1.5
tag v1.5
Tagger: Scott Chacon <[email protected]>
Date:   Mon Feb 9 15:22:20 2009 -0800 
my signed 1.5 tag
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
iEYEABECAAYFAkmQurIACgkQON3DxfchxFr5cACeIMN+ZxLKggJQf0QYiQBwgySN
Ki0An2JeAVUCAiJ7Ox6ZEtK+NvZAj82/
=WryJ
-----END PGP SIGNATURE-----
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon <[email protected]>
Date:   Sun Feb 8 19:02:46 2009 -0800
    Merge branch 'experiment'


  稍后我们会介绍如何验证签名标签

  轻量级标签

  轻量级标签实际上就是存在个文件中提交校验和--没有附加任何其他信息创建轻量级标签思路方法就是把上面'-a','-s','-m'这些选项都去掉

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5


  如果现在对这个标签使用'git show'命令不会看到像上面那种标签显示那么多内容仅仅显示这次提交有关信息

$ git show v1.4-lw
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon <[email protected]>
Date:   Sun Feb 8 19:02:46 2009 -0800
    Merge branch 'experiment'


  验证标签

  使用'git tag -v (tag)'就可以验证个签名标签了这个命令会用到GPG来验证签名前提是:你必须在密钥环中存放着签名者公钥

$ git tag -v v1.4.2.1
object 883653babd8ee7ea23e6a5c392bb739348b1eb61
type commit
tag v1.4.2.1
tagger Junio C Hamano <[email protected]> 1158138501 -0700 
GIT 1.4.2.1
Minor fixes since 1.4.2, including git-mv and git-http with alternates.
gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Good signature from "Junio C Hamano <[email protected]>"
gpg:                 aka "[jpeg image of size 1513]"
Primary key fingerpr: 3565 2A26 2040 E066 C9A7  4A7D C0C6 D9A4 F311 9B9A


  如果你没有这个公钥你会看到这样信息:

gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Can't check signature: public key not found
error: could not very the tag 'v1.4.2.1'


  后期贴标签

  这种情况是说你想对以前某次提交贴个标签如果整个提交历史是这样:

$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme


  忘记在那个提交信息为'updated rakefile'点上贴个'v1.2'标签了我可以现在贴个上去你可以在执行创建标签语句后面跟上那次提交校验和(或者部分校验和)

$ git tag -a v1.2 9fceb02

  现在我们可以列出所有标签:

$ git tag 
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5




$ git show v1.2
tag v1.2
Tagger: Scott Chacon <[email protected]>
Date:   Mon Feb 9 15:32:16 2009 -0800

version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <[email protected]>
Date:   Sun Apr 27 20:43:35 2008 -0700
    updated rakefile
...


  共享标签

  默认情况下'git push'命令不会将标签上传到远程服务器上为了共享这些标签你必须在'git push'命令后明确添加-tags选项

[master]$ git push --tags
Counting objects: 50, done.
Compressing objects: 100% (38/38), done.
Writing objects: 100% (44/44), 4.56 KiB, done.
Total 44 (delta 18), reused 8 (delta 1)
To [email protected]:schacon/simplegit.git
 * [ tag]         v0.1 -> v0.1
 * [ tag]         v1.2 -> v1.2
 * [ tag]         v1.4 -> v1.4
 * [ tag]         v1.4-lw -> v1.4-lw
 * [ tag]         v1.5 -> v1.5


  现在如果有人克隆或者在线同步你git仓库标签也会并同步了



标签:
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: