git tag命令

git tag命令用於創建,列出,刪除或驗證使用GPG簽名的標籤對象。同大多數 VCS 一樣,Git 也可以對某一時間點上的版本打上標籤。人們在發佈某個軟件版本(比如 v1.0 等等)的時候,經常這麼做。本節我們一起來學習如何列出所有可用的標籤,如何新建標籤,以及各種不同類型標籤之間的差別。

使用語法

git tag [-a | -s | -u <keyid>] [-f] [-m <msg> | -F <file>]
    <tagname> [<commit> | <object>]
git tag -d <tagname>…
git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]
    [--points-at <object>] [--column[=<options>] | --no-column]
    [--create-reflog] [--sort=<key>] [--format=<format>]
    [--[no-]merged [<commit>]] [<pattern>…]
git tag -v [--format=<format>] <tagname>…

描述

refs/tags/中添加標籤引用,除非提供了-d/-l/-v來刪除,列出或驗證標籤。
tag 用於創建一個標籤 用於在開發階段,某個階段的完成,創建一個版本,在開發中都會使用到, 可以創建一個tag來指向軟件開發中的一個關鍵時期,比如版本號更新的時候可以建一個version1.0, version1.2之類的標籤,這樣在以後回顧的時候會比較方便。tag的使用很簡單。

除非指定-f選項,否則不能創建已經存在的標籤。

如果傳遞了-a-s-u <keyid>中的一個,該命令將創建一個標籤對象,並且需要一個標籤消息。 除非-m <msg>-F <file>,否則將啓動一個編輯器,供用戶輸入標籤消息。

示例

以下是一些示例 -

1.列顯已有的標籤

列出現有標籤的命令非常簡單,直接運行 git tag 即可:

$ $ git tag
v1.0
v1.2

顯示的標籤按字母順序排列,所以標籤的先後並不表示重要程度的輕重。
我們可以用特定的搜索模式列出符合條件的標籤。在 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

2.創建標籤

Git 使用的標籤有兩種類型:輕量級的(lightweight)和含附註的(annotated)。輕量級標籤就像是個不會變化的分支,實際上它就是個指向特定提交對象的引用。而含附註標籤,實際上是存儲在倉庫中的一個獨立對象,它有自身的校驗和信息,包含着標籤的名字,電子郵件地址和日期,以及標籤說明,標籤本身也允許使用 GNU Privacy Guard (GPG) 來簽署或驗證。一般我們都建議使用含附註型的標籤,以便保留相關信息;當然,如果只是臨時性加註標籤,或者不需要旁註額外信息,用輕量級標籤也沒問題。

創建一個含附註類型的標籤非常簡單,用 -a (譯註:取 annotated 的首字母)指定標籤名字即可:

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

-m 選項則指定了對應的標籤說明,Git 會將此說明一同保存在標籤對象中。如果沒有給出該選項,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'

我們可以看到在提交對象信息上面,列出了此標籤的提交者和提交時間,以及相應的標籤說明。

3.簽署標籤

如果你有自己的私鑰,還可以用 GPG 來簽署標籤,只需要把之前的選項 -a 改爲 -s (譯註: 取 signed 的首字母)即可:

$ 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 簽名也附在其內:

$ 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'

4.刪除標籤

很簡單,比如想要名稱刪除名稱爲:v1.0的標籤,可以執行以下操作:

$ git tag -d v1.0

5.輕量級標籤

輕量級標籤實際上就是一個保存着對應提交對象的校驗和信息的文件。要創建這樣的標籤,一個 -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'

6.驗證標籤

可以使用 git tag -v [tag-name] (譯註:取 verify 的首字母)的方式驗證已經簽署的標籤。此命令會調用 GPG 來驗證簽名,所以你需要有簽署者的公鑰,存放在 keyring 中,才能驗證:

$ 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 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'

7.後期加註標籤

甚至可以在後期對早先的某次提交加註標籤。比如在下面展示的提交歷史中:

$ 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
...

8.分享標籤

默認情況下,git push 並不會把標籤傳送到遠端服務器上,只有通過顯式命令才能分享標籤到遠端倉庫。其命令格式如同推送分支,運行 git push origin [tagname] 即可:

$ git push origin 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]         v1.5 -> v1.5

如果要一次推送所有本地新增的標籤上去,可以使用 --tags 選項:

$ git push origin --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

現在,其他人克隆共享倉庫或拉取數據同步後,也會看到這些標籤。