git基础-打标签
打標(biāo)簽
像其他版本控制系統(tǒng)(VCS)一樣,Git 可以給歷史中的某一個(gè)提交打上標(biāo)簽,以示重要。 比較有代表性的是人們會(huì)使用這個(gè)功能來(lái)標(biāo)記發(fā)布結(jié)點(diǎn)(v1.0 等等)。 在本節(jié)中,你將會(huì)學(xué)習(xí)如何列出已有的標(biāo)簽、如何創(chuàng)建新標(biāo)簽、以及不同類型的標(biāo)簽分別是什么。
列出標(biāo)簽
在 Git 中列出已有的標(biāo)簽是非常簡(jiǎn)單直觀的。 只需要輸入?git tag:
$ git tag v0.1 v1.3這個(gè)命令以字母順序列出標(biāo)簽;但是它們出現(xiàn)的順序并不重要。
你也可以使用特定的模式查找標(biāo)簽。 例如,Git 自身的源代碼倉(cāng)庫(kù)包含標(biāo)簽的數(shù)量超過(guò) 500 個(gè)。 如果只對(duì) 1.8.5 系列感興趣,可以運(yùn)行:
$ git tag -l 'v1.8.5*' v1.8.5 v1.8.5-rc0 v1.8.5-rc1 v1.8.5-rc2 v1.8.5-rc3 v1.8.5.1 v1.8.5.2 v1.8.5.3 v1.8.5.4 v1.8.5.5創(chuàng)建標(biāo)簽
Git 使用兩種主要類型的標(biāo)簽:輕量標(biāo)簽(lightweight)與附注標(biāo)簽(annotated)。
一個(gè)輕量標(biāo)簽很像一個(gè)不會(huì)改變的分支 - 它只是一個(gè)特定提交的引用。
然而,附注標(biāo)簽是存儲(chǔ)在 Git 數(shù)據(jù)庫(kù)中的一個(gè)完整對(duì)象。 它們是可以被校驗(yàn)的;其中包含打標(biāo)簽者的名字、電子郵件地址、日期時(shí)間;還有一個(gè)標(biāo)簽信息;并且可以使用 GNU Privacy Guard (GPG)簽名與驗(yàn)證。 通常建議創(chuàng)建附注標(biāo)簽,這樣你可以擁有以上所有信息;但是如果你只是想用一個(gè)臨時(shí)的標(biāo)簽,或者因?yàn)槟承┰虿幌胍4婺切┬畔?#xff0c;輕量標(biāo)簽也是可用的。
附注標(biāo)簽
在 Git 中創(chuàng)建一個(gè)附注標(biāo)簽是很簡(jiǎn)單的。 最簡(jiǎn)單的方式是當(dāng)你在運(yùn)行?tag?命令時(shí)指定?-a?選項(xiàng):
$ git tag -a v1.4 -m 'my version 1.4' $ git tag v0.1 v1.3 v1.4-m?選項(xiàng)指定了一條將會(huì)存儲(chǔ)在標(biāo)簽中的信息。 如果沒(méi)有為附注標(biāo)簽指定一條信息,Git 會(huì)運(yùn)行編輯器要求你輸入信息。
通過(guò)使用?git show?命令可以看到標(biāo)簽信息與對(duì)應(yīng)的提交信息:
$ git show v1.4 tag v1.4 Tagger: Ben Straub <ben@straub.cc> Date: Sat May 3 20:19:12 2014 -0700my version 1.4commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700changed the version number輸出顯示了打標(biāo)簽者的信息、打標(biāo)簽的日期時(shí)間、附注信息,然后顯示具體的提交信息。
輕量標(biāo)簽
另一種給提交打標(biāo)簽的方式是使用輕量標(biāo)簽。 輕量標(biāo)簽本質(zhì)上是將提交校驗(yàn)和存儲(chǔ)到一個(gè)文件中 - 沒(méi)有保存任何其他信息。 創(chuàng)建輕量標(biāo)簽,不需要使用?-a、-s?或?-m?選項(xiàng),只需要提供標(biāo)簽名字:
$ git tag v1.4-lw $ git tag v0.1 v1.3 v1.4 v1.4-lw v1.5這時(shí),如果在標(biāo)簽上運(yùn)行?git show,你不會(huì)看到額外的標(biāo)簽信息。 命令只會(huì)顯示出提交信息:
$ git show v1.4-lw commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700changed the version number后期打標(biāo)簽
你也可以對(duì)過(guò)去的提交打標(biāo)簽。 假設(shè)提交歷史是這樣的:
$ 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現(xiàn)在,假設(shè)在 v1.2 時(shí)你忘記給項(xiàng)目打標(biāo)簽,也就是在 “updated rakefile” 提交。 你可以在之后補(bǔ)上標(biāo)簽。 要在那個(gè)提交上打標(biāo)簽,你需要在命令的末尾指定提交的校驗(yàn)和(或部分校驗(yàn)和):
$ git tag -a v1.2 9fceb02可以看到你已經(jīng)在那次提交上打上標(biāo)簽了:
$ 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 <schacon@gee-mail.com> Date: Mon Feb 9 15:32:16 2009 -0800version 1.2 commit 9fceb02d0ae598e95dc970b74767f19372d61af8 Author: Magnus Chacon <mchacon@gee-mail.com> Date: Sun Apr 27 20:43:35 2008 -0700updated rakefile ...共享標(biāo)簽
默認(rèn)情況下,git push?命令并不會(huì)傳送標(biāo)簽到遠(yuǎn)程倉(cāng)庫(kù)服務(wù)器上。 在創(chuàng)建完標(biāo)簽后你必須顯式地推送標(biāo)簽到共享服務(wù)器上。 這個(gè)過(guò)程就像共享遠(yuǎn)程分支一樣 - 你可以運(yùn)行?git push origin [tagname]。
$ git push origin v1.5 Counting objects: 14, done. Delta compression using up to 8 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done. Total 14 (delta 3), reused 0 (delta 0) To git@github.com:schacon/simplegit.git* [new tag] v1.5 -> v1.5如果想要一次性推送很多標(biāo)簽,也可以使用帶有?--tags?選項(xiàng)的?git push?命令。 這將會(huì)把所有不在遠(yuǎn)程倉(cāng)庫(kù)服務(wù)器上的標(biāo)簽全部傳送到那里。
$ git push origin --tags Counting objects: 1, done. Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done. Total 1 (delta 0), reused 0 (delta 0) To git@github.com:schacon/simplegit.git* [new tag] v1.4 -> v1.4* [new tag] v1.4-lw -> v1.4-lw現(xiàn)在,當(dāng)其他人從倉(cāng)庫(kù)中克隆或拉取,他們也能得到你的那些標(biāo)簽。
刪除標(biāo)簽
要?jiǎng)h除掉你本地倉(cāng)庫(kù)上的標(biāo)簽,可以使用命令?git tag -d <tagname>。例如,可以使用下面的命令刪除掉一個(gè)輕量級(jí)標(biāo)簽:
$ git tag -d v1.4-lw Deleted tag 'v1.4-lw' (was e7d5add)應(yīng)該注意的是上述命令并不會(huì)從任何遠(yuǎn)程倉(cāng)庫(kù)中移除這個(gè)標(biāo)簽,你必須使用?git push <remote> :refs/tags/<tagname>?來(lái)更新你的遠(yuǎn)程倉(cāng)庫(kù):
$ git push origin :refs/tags/v1.4-lw To /git@github.com:schacon/simplegit.git- [deleted] v1.4-lw檢出標(biāo)簽
如果你想查看某個(gè)標(biāo)簽所指向的文件版本,可以使用?git checkout?命令,雖然說(shuō)這會(huì)使你的倉(cāng)庫(kù)處于“分離頭指針(detacthed HEAD)”狀態(tài)——這個(gè)狀態(tài)有些不好的副作用:
$ git checkout 2.0.0 Note: checking out '2.0.0'.You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:git checkout -b <new-branch>HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final$ git checkout 2.0-beta-0.1 Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final HEAD is now at df3f601... add atlas.json and cover image在“分離頭指針”狀態(tài)下,如果你做了某些更改然后提交它們,標(biāo)簽不會(huì)發(fā)生變化,但你的新提交將不屬于任何分支,并且將無(wú)法訪問(wèn),除非確切的提交哈希。因此,如果你需要進(jìn)行更改——比如說(shuō)你正在修復(fù)舊版本的錯(cuò)誤——這通常需要?jiǎng)?chuàng)建一個(gè)新分支:
$ git checkout -b version2 v2.0.0 Switched to a new branch 'version2'當(dāng)然,如果在這之后又進(jìn)行了一次提交,version2?分支會(huì)因?yàn)檫@個(gè)改動(dòng)向前移動(dòng),version2?分支就會(huì)和?v2.0.0?標(biāo)簽稍微有些不同,這時(shí)就應(yīng)該當(dāng)心了。
轉(zhuǎn)載于:https://www.cnblogs.com/laphome/p/11280658.html
總結(jié)
- 上一篇: [转] 小结js屏幕、浏览器、页面大小(
- 下一篇: 动物笑话一则