标签管理:git tag

我们下载的软件或软件包一般会分为不同的版本,比如Linux内核,每隔一段时间,就会发布不同的版本,代表不同的里程碑:linux-2.6.30、Linux-4.4.1、Linux-5.4.11…..

这些软件版本号我们可以通过Git的标签功能来实现。Git标签本质上就是一个引用,标签对象可以指向任何对象,通常情况下是指向一个提交(commit):如果我们的软件截止到某个提交,已经有了里程碑的意义,就可以通过标签进行标记,比如发布对应的V1.0版本。

root@ubuntu:/home/test# git tag v1.0
root@ubuntu:/home/test# git tag
v1.0

命令git tag v1.0是基于当前分支的末端,即基于当前分支的最新提交为基点来创建一个标签。创建成功后我们可以通过git tag命令来查看创建的所有标签。如果你想基于某个具体的提交来创建一个标签,可以使用下面的命令:

root@ubuntu:/home/test# git tag v0.1 62d21534a
root@ubuntu:/home/test# git tag
v0.1
v1.0

比如我们基于test仓库的第一个commit提交来创建一个v0.1版本,就可以使用上面git tag v0.1 SHA1的命令格式,SHA1是指某一个具体提交的commit ID,即哈希值。

标签的本质可以看做是一个指针,指向某一个具体的commit提交。唯一不同的是,指针作为一个内存变量,存储在内存中,而标签则作为一个文件,存储在磁盘上的仓库中,文件中保存的就是其指向的提交的哈希值。在.git/refs/tags目录下可以看到我们创建的标签文件:

root@ubuntu:/home/test/.git/refs/tags# ls
v0.1  v1.0
root@ubuntu:/home/test/.git/refs/tags# cat v0.1 
62d21534ae082c61dc9e40196d5ff2265b4ca845
root@ubuntu:/home/test/.git/refs/tags# cat v1.0
bcb724d948d710f12fcfb084101598359988e438
root@ubuntu:/home/test/.git/refs/tags#

使用cat命令查看标签文件的值,你会发现,文件里保存里就是它指向的commit的哈希值。明白了标签的本质,我们就可以使用git show、git checkout命令对标签做各种操作了。比如我们想将当前的软件切换到v0.1版本,使用下面的命令就可以完成:

root@ubuntu:/home/test# git checkout v0.1
Note: checking out 'v0.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 62d2153 Init test repo and add test.c to repo

root@ubuntu:/home/test# git log
commit 62d21534ae082c61dc9e40196d5ff2265b4ca845 (HEAD, tag: v0.1)
Author: “litao.wang” <3284757626@qq.com>
Date:   Wed Sep 23 18:43:44 2020 -0700

    Init test repo and add test.c to repo

切换到v0.1版本后,使用git log命令查看提交信息,你会发现在当前版本下,就只有一个最初的提交了。根据提示信息,你可以以当前的版本为节点,去创建新的分支。

如果你想查看某一个标签的具体修改信息,可以使用git show命令:

root@ubuntu:/home/test# git show v1.0
commit bcb724d948d710f12fcfb084101598359988e438 (tag: v1.0, master)
Merge: a547cfb 589f10d
Author: litao.wang <3284757626@qq.com>
Date:   Wed Sep 23 20:42:01 2020 -0700

    master:fix merge conflicts

diff --cc test.c
index 24dbe41,2fd5879..212b702
--- a/test.c
+++ b/test.c
@@@ -1,12 -1,12 +1,17 @@@
  #include <stdio.h>

 +int func_a(void)
 +{
 +    return 0;
 +}
+ int func_b(void)
+ {
+     return 0;
+ }

  int main (void)
  {
 +    func_a();
+     func_b();
      return 0;
  }

通过这个命令,你会看到v1.0版本这个标签,跟前一个commit相比,到底做了哪些修改。

通过上面的学习我们可以看到:标签跟分支类似,也是一个引用,指向一个具体提交的哈希值,用来标识软件的版本或开发里程碑。我们在使用git进行操作时,使用标签,可以更好的辨识某个具体的提交,使用起来也更加方便。当然,根据需要你也可以删除它们:

oot@ubuntu:/home/test# git tag -d v1.0
Deleted tag 'v1.0' (was bcb724d)
root@ubuntu:/home/test# git tag
v0.1
root@ubuntu:/home/test# git tag -d v0.1
Deleted tag 'v0.1' (was 62d2153)
root@ubuntu:/home/test# git tag
root@ubuntu:/home/test#
《Linux三剑客》视频教程,从零开始快速掌握Linux开发常用的工具:Git、Makefile、vim、autotools、debug,免费赠送C语言视频教程,C语言项目实战:学生成绩管理系统。详情请点击淘宝链接:Linux三剑客