1. 程式人生 > >git標籤管理

git標籤管理

[toc] ## 備註: 本文參考於廖雪峰老師的部落格[Git教程](https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)。依照其部落格進行學習和記錄,感謝其無私分享,也歡迎各位檢視原文。 ## 知識點 - `git tag `新建一個標籤,預設表示的是`HEAD`,當前提交. - `git tag v0.9 commit_id`在指定提交上建立標籤 - `git tag -a -m "blablabla..."`指定標籤資訊; - `git tag -s -m "blablabla..."`用`PGP`簽名標籤; - 命令`git tag`檢視標籤列表。 - `git show `顯示標籤資訊 - 刪除標籤`git tag -d ` - 將本地標籤推送到遠端`git push origin ` - 一次性推送全部標籤到遠端`git push origin --tags` - 刪除遠端標籤`git push origin :refs/tags/` ## 標籤 - 標籤類似版本庫的一個快照。當釋出一個版本時,通常先在版本庫中打一個標籤(tag),用以唯一確定打標籤時刻的版本。 - 通過獲取某個標籤的版本,就可以把當時的歷史版本取出來 - Git的標籤本質也是指向某個`commit`的指標,類似於分支,但是標籤不能移動,分支可以移動。建立和刪除標籤都是瞬間完成 - Git標籤方便標識。因為原本的`commit`是一串數字(`6a5819e...`),但是`tag v1.2`就很方便查詢和標識 ## Git上打標籤 - 切換到需要打標籤的分支上: ```s $ git checkout master 切換到分支 'master' 您的分支與上游分支 'origin/master' 一致。 ``` - 建立標籤tag ```s $ git tag v1.0 ``` - 檢視標籤tag歷史 ```s $ git tag v1.0 ``` - 標籤`tag`預設打在最新提交的`commit`上。 如果忘了打標籤,可以查詢歷史提交的`commit_id`,在指定`commit_id`上打標籤 - 指定`commit_id`上打標籤,如下,在提交`merged fixed bug`上打新標籤 檢視`commit_id` ```s $ git log --pretty=oneline --abbrev-commit af1b0b3 add some word on master faaaaa6 merged fixed bug afc33ef fixed a bug 0df6e43 Merge branch 'dev' ``` 在`commit_id`上打標籤,檢視tag歷史 ```s $ git tag v0.9 faaaaa6 $ git tag v0.9 v1.0 ``` - `tag`標籤是按字母順序排序的,使用`git show `檢視標籤資訊 ```s $ git show v0.9 commit faaaaa6756a8d04c269b7b5ddccfc2a9e67108db Merge: 0df6e43 afc33ef Author: findmoon <[email protected]> Date: Wed Feb 21 22:25:03 2018 +0800 merged fixed bug ``` - 建立帶有說明的標籤,`-a`指定標籤名,`-m`指定標籤說明 ```s $ git tag -a v0.8 -m"version 0.8 released" 7c4d427 ``` - `git show`顯示標籤說明和資訊 ```s $ git show v0.8 tag v0.8 Tagger: findmoon <[email protected]> Date: Thu Feb 22 10:39:40 2018 +0800 version 0.8 released commit 7c4d4271b7bbd7a9898574ff3cfa795f40f9bbe3 Merge: 8f69de8 44dffc0 Author: findmoon <[email protected]> Date: Wed Feb 21 14:54:46 2018 +0800 merge with no-ff ``` - 使用`-s`引數,用私鑰簽名一個標籤 ```s $ git tag -s v0.7 -m"version 0.7 released" afc33ef gpg: 鑰匙環‘/home/liu/.gnupg/secring.gpg’已建立 gpg: 鑰匙環‘/home/liu/.gnupg/pubring.gpg’已建立 gpg: “findmoon <[email protected]>”已跳過:私鑰不可用 gpg: signing failed: 私鑰不可用 error: gpg 無法為資料簽名 error: 無法簽署標籤 ``` 因為簽名採用`PGP`簽名,所以必須首先安裝`gpg(GnuPG)`,沒有`gpg`或者`gpgp`金鑰對,就會報錯。 **用`PGP`簽名的標籤是不可偽造的** ## 標籤的管理 - 刪除標籤`git tag -d ` ```s $ git tag -d v0.8 已刪除標籤 'v0.8'(曾為 08825cf) ``` - 將本地標籤推送到遠端`git push origin ` ```s $ git push origin v1.0 Total 0 (delta 0), reused 0 (delta 0) To [email protected]:findmoon/newrepo.git * [new tag] v1.0 -> v1.0 ``` - 一次性推送全部為推送的標籤到遠端`git push origin --tags` ```s $ git push origin --tags 物件計數中: 1, 完成. 寫入物件中: 100% (1/1), 164 bytes | 0 bytes/s, 完成. Total 1 (delta 0), reused 0 (delta 0) To [email protected]:findmoon/newrepo.git * [new tag] 0.8 -> 0.8 * [new tag] v0.9 -> v0.9 ``` - 標籤推送到遠端後的刪除 1. 先刪除本地標籤 ```s $ git tag -d 0.8 已刪除標籤 '0.8'(曾為 4dcd55c) ``` 2. 刪除遠端標籤`git push origin :refs/tags/` ```s $ git push origin :refs/tags/0.8 To [email protected]:findmoon/newrepo.git - [deleted]