Git标签操作

Git标签操作

标签操作允许为存储库中的特定版本提供有意义的名称。 假设项目中有两个程序员:maxsuminsu,他们决定标记项目代码,以便以后可以更容易访问这些代码。

创建标签

使用git tag命令来标记当前HEAD指针。在创建标签时需要提供-a选项的标签名称,并提供带-m选项的标签消息。

$ pwd
/D/worksp/sample

Administrator@MY-PC /D/worksp/sample (master)
$ git tag -a 'Release_1_0' -m 'Tagged basic string operation code' HEAD

如果要标记特定提交,则使用相应的COMMIT ID而不是HEAD指针。使用以下命令将标签推送到远程存储库。

$ git push origin tag Release_1_0
Username for 'http://git.oschina.net': 769728683@qq.com
Password for 'http://769728683@qq.com@git.oschina.net':
Counting objects: 1, done.
Writing objects: 100% (1/1), 177 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To http://git.oschina.net/zyiz/sample.git
 * [new tag]         Release_1_0 -> Release_1_0

查看标签

假设开发人员(maxsu)创建了标签。 现在,另外一个开发人员(minsu)就可以使用带有-l选项的git tag命令查看所有可用的标签。

zyiz@ubuntu:~/git/sample$ pwd
/home/zyiz/git/sample

zyiz@ubuntu:~/git/sample$ git pull
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From http://git.oschina.net/zyiz/sample
 * [new tag]         Release_1_0 -> Release_1_0
Already up-to-date.

zyiz@ubuntu:~/git/sample$ git tag -l
Release_1_0
zyiz@ubuntu:~/git/sample$

可使用git show命令后跟其标签名来查看有关标签的更多详细信息。

$ git show Release_1_0
tag Release_1_0
Tagger: maxsu <your_email@mail.com>
Date:   Mon Jul 10 23:06:04 2017 +0800

Tagged basic string operation code

commit 44ea8e47307b47c9a80b44360e09f973e79312b0
Author: maxsu <your_email@mail.com>
Date:   Mon Jul 10 21:09:35 2017 +0800

    add new file string.py

diff --git a/src/string.py b/src/string.py
new file mode 100644
index 0000000..42fd1dd
--- /dev/null
+++ b/src/string.py
@@ -0,0 +1,7 @@
+#!/usr/bin/python3
+
+var1 = 'Hello World!'
+var2 = "Python Programming"
+
+print ("var1[0]: ", var1[0])
+print ("var2[1:5]: ", var2[1:5]) # 切片加索引
\ No newline at end of file

Administrator@MY-PC /D/worksp/sample (master)
$

删除标签

使用以下命令从本地以及远程存储库中删除标签,注意使用 git tag -d 中带有-d选项 -

$ git tag
Release_1_0

Administrator@MY-PC /D/worksp/sample (master)

$ git tag -d Release_1_0
Deleted tag 'Release_1_0' (was 600fa78)

Administrator@MY-PC /D/worksp/sample (master)

$ git push origin :Release_1_0
Username for 'http://git.oschina.net': 769728683@qq.com
Password for 'http://769728683@qq.com@git.oschina.net':
To http://git.oschina.net/zyiz/sample.git
 - [deleted]         Release_1_0

Administrator@MY-PC /D/worksp/sample (master)
$