发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。所以,标签也是版本库的一个快照。
Git的标签虽然是版本库的快照,但其实它就是指向某个commit的指针(跟分支很像对不对?但是分支可以移动,标签不能移动),所以,创建和删除标签都是瞬间完成的。
1. 切换到需要打标签的分支上打上标签

$ git branch
* dev
  master
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git tag v1.0
$ git tag
v1.0

git tag 查看所有标签



2. 给历史提交打标签


默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,可以找到历史提交的commit id,然后打上就可以了


$  git log --pretty=oneline --abbrev-commit
b0e9ec8 delete test
120dab8 add test.txt
1161148 tracks change
75fa7b7 append GPL
85e468e free software
a2de034 git init
$ git tag v0.9 1161148
$ git tag
v0.9
v1.0
$ git show v0.9
commit 1161148cb240393c12226419c88f155751258e79
Author: morris131 <morris131@163.com>
Date:   Thu Dec 17 09:57:31 2015 +0800
    tracks change
diff --git a/readme.txt b/readme.txt
index 8968467..5cedd61 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 git is a version control system.
-git is free software under the GPL.
\ No newline at end of file
+git is free software under the GPL.
+git tracks changes.
\ No newline at end of file

git show <tagname> 显示标签信息



3. 创建带有说明的标签


$ git tag -a v0.1 -m "version 0.1" a2de034
$ git show v0.1
tag v0.1
Tagger: morris131 <morris131@163.com>
Date:   Tue Dec 22 09:12:18 2015 +0800
version 0.1
commit a2de034c5e9a9fda86d7ed7005265493cd2a054b
Author: morris131 <morris131@163.com>
Date:   Wed Dec 16 13:44:53 2015 +0800
    git init
diff --git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..260aa42
--- /dev/null
+++ b/readme.txt
@@ -0,0 +1 @@
+git is a version control system.
\ No newline at end of file