在Git中打标签非常简单,首先,切换到需要打标签的分支上:
$ git branch
* dev
master
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
然后,敲命令git tag <name>
就可以打一个新标签:
$ git tag v1.0
可以用命令git tag
查看所有标签:
$ git tag
v1.0
默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,比如,现在已经是周五了,但应该在周一打的标签没有打,怎么办?
方法是找到历史提交的commit id,然后打上就可以了:
$ git log --pretty=oneline --abbrev-commit
12281ee (HEAD -> master, tag: v1.0, origin/master) merged bug fix 101
ba56780 fix bug 101
1353718 merge with no-ff
384ee10 add merge
823d77a conflict fixed
8e1c627 & simple
cb1f0fc AND simple
b001a02 branch test
af4b286 add test.txt
c0a5ce8 remove test.txt
d31ddf5 add test.txt
fb315a3 git tracks changes
9ed54e1 understand how stage works
5aaf247 append GPL
d4ab026 add distributed
effe98f write a ReadMe file
比方说要对add merge
这次提交打标签,它对应的commit id是384ee10
,敲入命令:
$ git tag v0.9 384ee10
再用命令git tag
查看标签:
$ git tag
v0.9
v1.0
注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show <tagname>
查看标签信息:
$ git show v0.9
commit 384ee1070afb24418f2c24638cce7c58aeede5db (tag: v0.9)
Author: zhangchao <nuaazc2011@163.com>
Date: Mon Jul 2 22:06:20 2018 +0800
add merge
diff --git a/ReadMe.txt b/ReadMe.txt
index 445d430..3d7dd3f 100644
--- a/ReadMe.txt
+++ b/ReadMe.txt
@@ -2,4 +2,5 @@ Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
-Creating a new branch is quick and simple.
\ No newline at end of file
+Creating a new branch is quick and simple.
+I am a student.
可以看到,v0.9
确实打在add merge
这次提交上。
还可以创建带有说明的标签,用-a
指定标签名,-m
指定说明文字:
$ git tag -a v0.1 -m "version 0.1 released" 5aaf247
用命令git show <tagname>
可以看到说明文字:
$ git show v0.1
tag v0.1
Tagger: zhangchao <nuaazc2011@163.com>
Date: Tue Jul 3 09:06:03 2018 +0800
version 0.1 released
commit 5aaf24759f50128496018b06407519bac9d8ea64 (tag: v0.1)
Author: zhangchao <nuaazc2011@163.com>
Date: Mon Jul 2 17:33:57 2018 +0800
append GPL
diff --git a/ReadMe.txt b/ReadMe.txt
index 9247db6..8443d23 100644
--- a/ReadMe.txt
+++ b/ReadMe.txt
@@ -1,2 +1,2 @@
Git is a distributed version control system.
-Git is free software.
+Git is free software distributed under the GPL.
注意:标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。
小结
命令
git tag <tagname>
用于新建一个标签,默认为HEAD
,也可以指定一个commit id;命令
git tag -a <tagname> -m "blablabla..."
可以指定标签信息;命令
git tag
可以查看所有标签。