在Git中打標簽非常簡單,首先,切換到需要打標簽的分支上:
$ git branch
* dev
master
$ git checkout master
Switched to branch 'master'
然后,敲命令git tag 就可以打一個新標簽:
$ git tag v1.0
可以用命令git tag查看所有標簽:
$ git tag
v1.0
默認標簽是打在最新提交的commit上的。有時候,如果忘了打標簽,比如,現在已經是周五了,但應該在周一打的標簽沒有打,怎么辦?
方法是找到歷史提交的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
比方說要對add merge這次提交打標簽,它對應的commit id是f52c633,敲入命令:
$ git tag v0.9 f52c633
再用命令git tag查看標簽:
$ git tag
v0.9
v1.0
注意,標簽不是按時間順序列出,而是按字母排序的??梢杂胓it show 查看標簽信息:
$ git show v0.9
commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9)
Author: Michael Liao <[email protected]>
Date: Fri May 18 21:56:54 2018 +0800
add merge
diff --git a/readme.txt b/readme.txt
...
可以看到,v0.9確實打在add merge這次提交上。
還可以創建帶有說明的標簽,用-a指定標簽名,-m指定說明文字:
$ git tag -a v0.1 -m "version 0.1 released" 1094adb
用命令git show 可以看到說明文字:
$ git show v0.1
tag v0.1
Tagger: Michael Liao <[email protected]>
Date: Fri May 18 22:48:43 2018 +0800
version 0.1 released
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (tag: v0.1)
Author: Michael Liao <[email protected]>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
diff --git a/readme.txt b/readme.txt
...
小結
⒈ 命令git tag <tagname>用于新建一個標簽,默認為HEAD,也可以指定一個commit id;
⒉ 命令git tag -a <tagname>-m "blablabla..."可以指定標簽信息;
⒊ 命令git tag可以查看所有標簽。