小結(jié)1
- 命令
git tag <tagname>
用于新建一個(gè)標(biāo)簽,默認(rèn)為HEAD扯旷,也可以指定一個(gè)commit id; - 命令
git tag -a <tagname> -m "blablabla..."
可以指定標(biāo)簽信息霸奕; - 命令
git tag
可以查看所有標(biāo)簽轴咱。
小結(jié)2
- 命令
git push origin <tagname>
可以推送一個(gè)本地標(biāo)簽;
2 .命令git push origin --tags
可以推送全部未推送過的本地標(biāo)簽剪廉; - 命令
git tag -d <tagname>
可以刪除一個(gè)本地標(biāo)簽娃循; - 命令
git push origin :refs/tags/<tagname>
可以刪除一個(gè)遠(yuǎn)程標(biāo)簽。
正文
Git的標(biāo)簽雖然是版本庫的快照斗蒋,但其實(shí)它就是指向某個(gè)commit的指針捌斧。tag就是一個(gè)讓人容易記住的有意義的名字,它跟某個(gè)commit綁在一起泉沾。
創(chuàng)造標(biāo)簽
在Git中打標(biāo)簽非常簡單捞蚂,首先,切換到需要打標(biāo)簽的分支上:
$ git branch
* dev
master
$ git checkout master
Switched to branch 'master'
然后跷究,敲命令git tag <name>
就可以打一個(gè)新標(biāo)簽:
可以用命令git tag
查看所有標(biāo)簽:
$ git tag v1.0
$ git tag
v1.0
默認(rèn)標(biāo)簽是打在最新提交的commit上的姓迅。
應(yīng)該在周一打的標(biāo)簽沒有打,怎么辦俊马?
方法是找到歷史提交的commit id丁存,然后打上就可以了:
$ git log --pretty=oneline --abbrev-commit
12a631b (HEAD -> master, tag: v1.0, origin/master) merged bug fix 101
4c805e2 fix bug 101
e1e9c68 merge with no-ff
f52c633 add merge
cf810e4 conflict fixed
5dc6824 & simple
14096d0 AND simple
b17d20e branch test
d46f35e remove test.txt
b84166e add test.txt
519219b git tracks changes
e43a48b understand how stage works
1094adb append GPL
e475afc add distributed
eaadf4e wrote a readme file
比方說要對(duì)add merge這次提交打標(biāo)簽,它對(duì)應(yīng)的commit id是f52c633柴我,敲入命令:git tag v0.9 f52c633
再用命令git tag查看標(biāo)簽:
$ git tag
v0.9
v1.0
注意解寝,標(biāo)簽不是按時(shí)間順序列出,而是按字母排序的艘儒×祝可以用git show <tagname>
查看標(biāo)簽信息:
$ git show v0.9
commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:56:54 2018 +0800
add merge
diff --git a/readme.txt b/readme.txt
...
可以看到夫偶,v0.9確實(shí)打在add merge這次提交上。
還可以創(chuàng)建帶有說明的標(biāo)簽嘉抓,用-a指定標(biāo)簽名索守,-m指定說明文字:
$ git tag -a v0.1 -m "version 0.1 released" 1094adb
用命令git show <tagname>
可以看到說明文字:
$ git show v0.1
tag v0.1
Tagger: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 22:48:43 2018 +0800
version 0.1 released
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (tag: v0.1)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
diff --git a/readme.txt b/readme.txt
...
注意:標(biāo)簽總是和某個(gè)commit掛鉤。如果這個(gè)commit既出現(xiàn)在master分支抑片,又出現(xiàn)在dev分支卵佛,那么在這兩個(gè)分支上都可以看到這個(gè)標(biāo)簽。
操作標(biāo)簽
如果標(biāo)簽打錯(cuò)了敞斋,也可以刪除:
$ git tag -d v0.1
Deleted tag 'v0.1' (was f15b0dd)
因?yàn)閯?chuàng)建的標(biāo)簽都只存儲(chǔ)在本地截汪,不會(huì)自動(dòng)推送到遠(yuǎn)程。所以植捎,打錯(cuò)的標(biāo)簽可以在本地安全刪除衙解。
推送某個(gè)標(biāo)簽到遠(yuǎn)程,使用命令git push origin <tagname>:
$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
* [new tag] v1.0 -> v1.0
或者焰枢,一次性推送全部尚未推送到遠(yuǎn)程的本地標(biāo)簽:
$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To github.com:michaelliao/learngit.git
* [new tag] v0.9 -> v0.9
如果標(biāo)簽已經(jīng)推送到遠(yuǎn)程蚓峦,要?jiǎng)h除遠(yuǎn)程標(biāo)簽就麻煩一點(diǎn),先從本地刪除:
$ git tag -d v0.9
Deleted tag 'v0.9' (was f52c633)
然后济锄,從遠(yuǎn)程刪除暑椰。刪除命令也是push,但是格式如下:
$ git push origin :refs/tags/v0.9
To github.com:michaelliao/learngit.git
- [deleted] v0.9
要看看是否真的從遠(yuǎn)程庫刪除了標(biāo)簽荐绝,可以登陸GitHub查看一汽。