Git学习笔记:标签管理

Git tag

如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。

Git tag 给当前分支打标签,一般是软件发版本前使用。这样就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。
标签也是版本库的一个快照。它虽然是版本库的快照,但其实它就是指向某个 commit 的指针(跟分支很像,但是分支可以移动,标签不能移动),所以,创建和删除标签都是瞬间完成的。

创建标签

Git支持两种标签:轻量标签(lightweight)与附注标签(annotated)。

轻量级标签就像是个不会变化的分支,实际上它就是个指向特定提交对象的引用。
而含附注标签,实际上是存储在仓库中的一个独立对象,它有自身的校验和信息,包含着标签的名字,电子邮件地址和日期,以及标签说明,标签本身也允许使用GNU Privacy Guard (GPG)来签署或验证。一般我们都建议使用含附注型的标签,以便保留相关信息;当然,如果只是临时性加注标签,或者不需要旁注额外信息,用轻量级标签也没问题。

创建一个含附注类型的标签非常简单,用-a(译注:取annotated的首字母)指定标签名字即可,如下:

$git tag -a tag_annotated_v1.0 -m "This is have comments tag"

创建一个轻量标签,直接使用:

$git tag tag_lightweight_v1.0

附注标签

codemaxi@codemaxi-PC:~/git_test$ git tag -a tag_annotated_v1.0 -m "This is have comments tag"
codemaxi@codemaxi-PC:~/git_test$ 
codemaxi@codemaxi-PC:~/git_test$ git tag 
tag_annotated_v1.0

-m选项指定了一条将会存储在标签中的备注说明信息。 如果没有用-m为附注标签指定一条信息,Git会启动编辑器要求你输入信息。否则创建标签失败。

创建成功后,可以通过使用git show tagName命令可以看到标签信息和与之对应的提交信息。
从下面的打印信息看到输出显示了打标签者的信息、打标签的日期时间、附注信息,然后显示具体的提交信息。

codemaxi@codemaxi-PC:~/git_test$ git show tag_annotated_v1.0 
tag tag_annotated_v1.0
Tagger: codemaxi <374867193@qq.com>
Date:   Sun May 30 09:36:23 2021 +0800

This is have comments tag

commit a8d38c4eca3245e489ef1684322e5a68c6265a8b (HEAD -> develop, tag: tag_annotated_v1.0)
Author: codemaxi <374867193@qq.com>
Date:   Sun May 23 22:08:15 2021 +0800

    direct commit without add

diff --git a/test.txt b/test.txt
index ab51f65..21cff5b 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 test-second line
+test files third line
codemaxi@codemaxi-PC:~/git_test$ 

轻量标签

另一种给提交打标签的方式是使用轻量标签。 轻量标签本质上是将提交校验和存储到一个文件中——没有保存任何其他信息。 创建轻量标签,不需要使用 -a、-s 或 -m 选项,只需要提供标签名字:

codemaxi@codemaxi-PC:~/git_test$ git tag tag_lightweight_v1.0
codemaxi@codemaxi-PC:~/git_test$ git tag 
tag_annotated_v1.0
tag_lightweight_v1.0
codemaxi@codemaxi-PC:~/git_test$ 

这时,如果在标签上运行git show tagName,你不会看到额外的标签信息。 命令只会显示出提交信息:

codemaxi@codemaxi-PC:~/git_test$ git show tag_lightweight_v1.0 
commit a8d38c4eca3245e489ef1684322e5a68c6265a8b (HEAD -> develop, tag: tag_lightweight_v1.0, tag: tag_annotated_v1.0)
Author: codemaxi <374867193@qq.com>
Date:   Sun May 23 22:08:15 2021 +0800

    direct commit without add

diff --git a/test.txt b/test.txt
index ab51f65..21cff5b 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 test-second line
+test files third line
codemaxi@codemaxi-PC:~/git_test$ 

后期打标签

你也可以在后期对早先的某次提交加注标签。比如在下面展示的提交历史中:

codemaxi@codemaxi-PC:~/git_test$ git log --pretty=oneline 
a8d38c4eca3245e489ef1684322e5a68c6265a8b (HEAD -> develop, tag: tag_lightweight_v1.0, tag: tag_annotated_v1.0) direct commit without add
3c09c3da5e686cc4d3cba4576d146f8824419cf5 new files test2.txt commit
577e8c0826b6185efff1c000d6828dcbd044786a test file second commit
0a78faca73c6742efd5029ba4b697c928b33b96d commit test.txt file

现在,假设在v0.5时你忘记给项目打标签,也就是在 “test file second commit” 提交。 你可以在之后补上标签。 要在那个提交上打标签,你需要在命令的末尾指定提交的校验和(或部分校验和):

codemaxi@codemaxi-PC:~/git_test$ git tag -a tag_v0.5 577e8c0826b6185efff1c000d6828dcbd044786a
codemaxi@codemaxi-PC:~/git_test$ git tag 
tag_annotated_v1.0
tag_lightweight_v1.0
tag_v0.5
codemaxi@codemaxi-PC:~/git_test$ 

上面我们打标签没有使用-m参数,就会弹出git编辑器让你输入信息了。

下面我们用git show tag_v0.5就可以看到标签具体信息了:

codemaxi@codemaxi-PC:~/git_test$ git show tag_v0.5 
tag tag_v0.5
Tagger: codemaxi <374867193@qq.com>
Date:   Sun May 30 09:51:31 2021 +0800

Tag lebel later

commit 577e8c0826b6185efff1c000d6828dcbd044786a (tag: tag_v0.5)
Author: codemaxi <374867193@qq.com>
Date:   Sun May 23 21:19:05 2021 +0800

    test file second commit

diff --git a/test.txt b/test.txt
index 08fe272..ab51f65 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-first line
+test-second line
codemaxi@codemaxi-PC:~/git_test$ 

删除标签

想要删除标签,很简单,比如想要名称删除名称为:v1.0的标签,可以执行以下操作:

$git tag -d v1.0

验证如下:

codemaxi@codemaxi-PC:~/git_test$ git tag 
tag_annotated_v1.0
tag_lightweight_v1.0
tag_v0.5
codemaxi@codemaxi-PC:~/git_test$ git tag -d tag_annotated_v1.0 
Deleted tag 'tag_annotated_v1.0' (was 3267b46)
codemaxi@codemaxi-PC:~/git_test$ git tag 
tag_lightweight_v1.0
tag_v0.5
codemaxi@codemaxi-PC:~/git_test$ 

检出标签

标签的目的不言而喻,就是记录一个提交的快照,方便后面可以直接取出该版本。
我们上面有提过,标签类似分支,所以如果想要检出某个标签对应的版本可以使用git checkout tagName命令, 虽然这会使你的仓库处于“分离头指针(detached HEAD)”的状态——这个状态有些不好的副作用:

codemaxi@codemaxi-PC:~/git_test$ git checkout tag_v0.5 
Note: checking out 'tag_v0.5'.

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 577e8c0 test file second commit
codemaxi@codemaxi-PC:~/git_test$ 

在“分离头指针”状态下,如果你想在这个版本上继续修改一些东西,然后提交,标签不会发生变化,而且你新修改提交的内容将不属于任何分支,并且将无法访问,必须要通过确切的提交哈希才能访问。

因此,这种情况下,如果你必须需要进行更改,比如你要修复旧版本中的错误,那么通常需要创建一个新分支,如下:

codemaxi@codemaxi-PC:~/git_test$ git checkout -b version-0.5 tag_v0.5 
Switched to a new branch 'version-0.5'
codemaxi@codemaxi-PC:~/git_test$ 

上面就是基于标签tag_v0.5对应的版本创建了一个分支,后面所有的修改都可以提交到这个分支而不影响其他的。这时候也体现了Git分支又强大又灵活,“一切皆分支”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值