Git笔记(9) 打标签


1. 标签

Git 可以给历史中的某一个提交打上标签,以示重要
比较有代表性的是人们会使用这个功能来标记发布结点,比如 v1.0 等等


2. 列出

只需要输入 git tag

$ git tag
v0.1
v1.3

这个命令 以字母顺序列 出标签

也可以使用 特定的模式查找 标签
例如,Git 自身的源代码仓库包含标签的数量超过xxx个
如果只对现在的 2.25.0 系列感兴趣,可以运行:

$ git tag -l 'v2.25.0*'

3. 创建

Git 使用两种主要类型的标签:

  • 轻量标签(lightweight)
  • 附注标签(annotated)

一个轻量标签很像一个不会改变的分支——它只是一个 特定提交的引用

然而,附注标签是存储在 Git 数据库中的一个 完整对象
它们是可以被校验的
其中包含打标签者的名字、电子邮件地址、日期时间,还有一个标签信息
并且可以使用 GNU Privacy Guard (GPG)签名与验证

通常建议创建附注标签,这样可以拥有以上所有信息

但是如果只是想用一个临时的标签,或者因为某些原因不想要保存那些信息
轻量标签也是可用的


3.1. 附注标签

在运行 tag 命令时指定 -a 选项,创建一个附注标签:

$ git tag -a v1.4 -m "my version 1.4"
$ git tag
v0.1
v1.3
v1.4

-m 选项指定了一条将会存储在标签中的信息
如果没有为附注标签指定一条信息,Git 会运行编辑器要求输入信息

通过使用 git show 命令可以看到标签信息与对应的提交信息:

$ git show v1.4
tag v1.4
Tagger: Ben Straub <[email protected]>
Date:   Sat May 3 20:19:12 2014 -0700

my version 1.4

commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

输出显示了打标签者的信息、打标签的日期时间、附注信息和具体的提交信息


3.2. 轻量标签

轻量标签本质上是 将提交校验和存储到一个文件中
没有保存任何其他信息

创建轻量标签,不需要使用 -a-s-m 选项,只需要提供标签名字:

$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5

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

$ git show v1.4-lw
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

4. 后期打标签

假设提交历史是这样的:

$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme

现在,假设在 v1.2 时忘记给项目打标签,也就是在 “updated rakefile” 提交
可以在之后补上标签
需要在命令的末尾指定提交的 校验和(或部分校验和):

$ git tag -a v1.2 9fceb02

可以看到已经在那次提交上打上标签了:

$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5

$ git show v1.2
tag v1.2
Tagger: Scott Chacon <[email protected]>
Date:   Mon Feb 9 15:32:16 2009 -0800

version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <[email protected]>
Date:   Sun Apr 27 20:43:35 2008 -0700

    updated rakefile
...

5. 共享

默认情况下,git push 命令并不会传送标签到远程仓库服务器上
在创建完标签后必须显式地推送标签到共享服务器上

这个过程就像共享远程分支一样
可以运行 git push origin [tagname]

$ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To [email protected]:schacon/simplegit.git
 * [new tag]         v1.5 -> v1.5

如果想要 批量推送 很多标签,也可以使用带有 --tags 选项的 git push 命令
这将会把所有不在远程仓库服务器上的标签全部传送到那里

$ git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To [email protected]:schacon/simplegit.git
 * [new tag]         v1.4 -> v1.4
 * [new tag]         v1.4-lw -> v1.4-lw

现在,当其他人从仓库中克隆或拉取,也能得到自己的那些标签


6. 删除

可以使用命令 git tag -d <tagname> 要删除掉本地仓库上的标签
例如,可以使用下面的命令删除掉一个轻量级标签:

$ git tag -d v1.4-lw
Deleted tag 'v1.4-lw' (was e7d5add)

注意上述命令并不会从任何远程仓库中移除这个标签
必须使用 git push <remote> :refs/tags/<tagname> 来更新远程仓库:

$ git push origin :refs/tags/v1.4-lw
To /[email protected]:schacon/simplegit.git
 - [deleted]         v1.4-lw

7. 检出

如果想查看某个标签所指向的文件版本,可以使用 git checkout 命令

虽然说这会使仓库处于“分离头指针(detacthed HEAD)”状态
这个状态有些不好的副作用
如果做了某些更改然后提交它们,标签不会发生变化
新提交将不属于任何分支,并且将无法访问,除非确切的提交哈希

$ git checkout 2.0.0
Note: checking out '2.0.0'.

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>

HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final

再最新内容时则不会:

$ git checkout 2.0-beta-0.1
Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final
HEAD is now at df3f601... add atlas.json and cover image

因此,如果需要进行更改
比如说正在修复 旧版本 的错误时
这通常需要创建一个新分支,还可以带上标签

$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'

当然,如果在这之后又进行了一次提交,version2 分支会因为这个改动向前移动
version2 分支 就会和 v2.0.0 标签 稍微有些不同,这时就应该当心了


参考: git

以上内容,均根据git官网介绍删减、添加和修改组成


相关推荐:

Git笔记(8) 远程仓库的使用
Git笔记(7) 撤消操作
Git笔记(6) 查看提交历史
Git笔记(5) 状态记录
Git笔记(4) 获取仓库


谢谢

发布了231 篇原创文章 · 获赞 299 · 访问量 294万+

猜你喜欢

转载自blog.csdn.net/qq_32618327/article/details/104268919