第五課 Git標籤
內容提要:創建有簽名,無簽名、輕量級標籤來永久的標記項目歷史中的關鍵點
跟大多數的VCS工具一樣,git也有在歷史狀態的關鍵點“貼標籤”的功能--一般人們用這個功能來標記發佈點(例如'v1.0')。這節課我們學習如何使用標籤列表,創建新標籤,以及在git中有哪些不同類別的標籤。
列出git中現有標籤
要想列出git中現有的所有標籤,輸入'git tag'命令運行即可:
$ git tag
v0.1
v1.3
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
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
$ git tag
v0.1
v1.3
v1.4
'-m'指明標籤信息,跟標籤一起存儲。如果你不使用-m指明標籤信息,git會自動啓動文本編輯器讓你輸入。
可以使用 git show 命令查看相應標籤的版本信息,並連同顯示打標籤時的提交對象。
my version 1.4
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon <[email protected]>
Date: Sun Feb 8 19:02:46 2009 -0800
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
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
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)
-----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
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 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'
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
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 p_w_picpath of size 1513]"
Primary key fingerprint: 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: Good signature from "Junio C Hamano <[email protected]>"
gpg: aka "[jpeg p_w_picpath of size 1513]"
Primary key fingerprint: 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 verify the tag 'v1.4.2.1'
gpg: Can't check signature: public key not found
error: could not verify 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
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
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5
version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <[email protected]>
Date: Sun Apr 27 20:43:35 2008 -0700
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
* [new tag] v0.1 -> v0.1
* [new tag] v1.2 -> v1.2
* [new tag] v1.4 -> v1.4
* [new tag] v1.4-lw -> v1.4-lw
* [new tag] v1.5 -> v1.5
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
* [new tag] v0.1 -> v0.1
* [new tag] v1.2 -> v1.2
* [new tag] v1.4 -> v1.4
* [new tag] v1.4-lw -> v1.4-lw
* [new tag] v1.5 -> v1.5
現在,如果有人克隆或者在線同步你的git倉庫的話,標籤也會一併同步了。