git tag (打标签)

Git has the ability to tag specific points in a repository’s history as being important. Typically, people use this functionality to mark release points (v1.0, v2.0 and so on).
Git 可以给仓库历史中的某一个提交打上标签,以示重要。比较有代表性的是人们会使用这个功能来标记发布结点 (v1.0、v2.0 等等)。

1. tag 与 branch 的区别

tag 对应某一次 commit,是一个点,只能查看,不可移动。tag 实现标记的功能,是 Git 版本库的一个快照,指向某个 commit 的指针。

branch 对应一系列 commit,是很多点连成的一根线,有一个 HEAD 指针,是可以依靠 HEAD 指针移动的。

tag 和 branch 相互配合使用,例如已经发布了 v1.0 v2.0 v3.0 等版本,在不改变现有代码的前提下,在 v2.0 的基础上加个新功能,作为 v4.0 发布。就可以检出 v2.0 的代码作为一个 branch,然后作为开发分支。改动代码用 branch ,不改动只查看用 tag。稳定版本备份用 tag,新功能多人开发用 branch (开发完成后 merge 到 master)。

tag 就像是一个里程碑,每一个标志一个点,branch 是一个新的征程一条线。tag 是静态的,branch 要向前开发。

  • master

The default development branch. Whenever you create a Git repository, a branch named “master” is created, and becomes the active branch. In most cases, this contains the local development, though that is purely by convention and is not required.
默认开发分支。每当您创建 Git repository 时,都会创建一个名为 master 的分支,并成为活动分支。在大多数情况下,这包含本地开发,尽管这纯粹是按照惯例并且不是必需的。

  • topic branch

A regular Git branch that is used by a developer to identify a conceptual line of development. Since branches are very easy and inexpensive, it is often desirable to have several small branches that each contain very well defined concepts or small incremental yet related changes.
一个常规的 Git 分支,开发人员使用它来识别开发的概念线。由于分支非常简单且成本低廉,因此通常希望有几个小分支,每个分支都包含定义非常明确的概念或小的增量但相关的更改。

  • branch

A “branch” is a line of development. The most recent commit on a branch is referred to as the tip of that branch. The tip of the branch is referenced by a branch head, which moves forward as additional development is done on the branch. A single Git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the “current” or “checked out” branch), and HEAD points to that branch.
分支是一条开发线。分支上的最新提交称为该分支的尖端。分支的尖端由分支头引用,随着在分支上完成额外的开发,它会向前移动。单个 Git 存储库可以跟踪任意数量的分支,但您的工作树仅与其中一个关联 (the “current” or “checked out” branch),并且 HEAD 指向该分支。

  • tag

A ref under refs/tags/ namespace that points to an object of an arbitrary type (typically a tag points to either a tag or a commit object). In contrast to a head, a tag is not updated by the commit command. A Git tag has nothing to do with a Lisp tag (which would be called an object type in Git’s context). A tag is most typically used to mark a particular point in the commit ancestry chain.
refs/tags/ 命名空间下的 ref 指向任意类型的对象 (通常标签指向标签或提交对象)。与头相比,提交命令不会更新标签。Git 标签与 Lisp 标签 (在 Git 的上下文中称为对象类型) 无关。 标签最常用于标记 commit ancestry chain 中的特定点。

  • tag object

An object containing a ref pointing to another object, which can contain a message just like a commit object. It can also contain a (PGP) signature, in which case it is called a “signed tag object”.
它还可以包含 (PGP) 签名,在这种情况下,它被称为签名标签对象。

2. Listing Your Tags (列出标签)

Listing the existing tags in Git is straightforward. Just type git tag (with optional -l or --list).
在 Git 中列出已有的标签非常简单,只需要输入 git tag (可带上可选的 -l 选项 --list):

(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git tag
0.12.0-rc0
0.12.0-rc1
0.12.1
0.5.0
0.6.0
tflite-v0.1.7
v0.10.0
v0.10.0rc0
v0.11.0
v0.11.0rc0
v0.11.0rc1
v0.11.0rc2
v0.12.0
v0.6.0
v0.7.0
v0.7.1
v0.8.0
v0.8.0rc0
v0.9.0
v0.9.0rc0
v1.0.0
v1.0.0-alpha
v1.0.0-rc0
v1.0.0-rc1
v1.0.0-rc2
v1.0.1
...

This command lists the tags in alphabetical order; the order in which they are displayed has no real importance.
这个命令以字母顺序列出标签,但是它们显示的顺序并不重要。

You can also search for tags that match a particular pattern.
你也可以按照特定的模式查找标签。如果只对 2.8 系列感兴趣,可以运行:

(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git tag -l "v2.8.*"
v2.8.0
v2.8.0-rc0
v2.8.0-rc1
v2.8.1
v2.8.2
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$

按照通配符列出标签需要 -l--list 选项。如果你只想要完整的标签列表,那么运行 git tag 就会默认假定你想要一个列表,它会直接给你列出来, 此时的 -l--list 是可选的。

如果你提供了一个匹配标签名的通配模式,那么 -l--list 就是强制使用的。

3. Creating Tags (创建标签)

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

A lightweight tag is very much like a branch that doesn’t change - it’s just a pointer to a specific commit.
轻量标签很像一个不会改变的分支,它只是某个特定提交的引用。

Annotated tags, however, are stored as full objects in the Git database.
而附注标签是存储在 Git 数据库中的一个完整对象,它们是可以被校验的,其中包含打标签者的名字、电子邮件地址、日期时间,此外还有一个标签信息,并且可以使用 GNU Privacy Guard (GPG) 签名并验证。通常会建议创建附注标签,这样你可以拥有以上所有信息。但是如果你只是想用一个临时的标签,或者因为某些原因不想要保存这些信息,那么也可以用轻量标签。

3.1 附注标签

在 Git 中创建附注标签十分简单。最简单的方式是当你在运行 git tag 命令时指定 -a 选项:

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

The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can type it in.
-m 选项指定了一条将会存储在标签中的信息。如果没有为附注标签指定一条信息,Git 会启动编辑器要求你输入信息。

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

(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git show v2.8.2
commit 2ea19cbb575d076b4f521d3603211c8316ad5f8f (HEAD, tag: v2.8.2, origin/r2.8)
Merge: 3a87370f889 d96d58fce11
Author: Mihai Maruseac <mihaimaruseac@google.com>
Date:   Sun May 22 15:24:29 2022 -0700Merge pull request #56213 from tensorflow/mm-disable-tests-on-r2.8Disable flaky tests(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$

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

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 <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700changed the version number

3.3 后期打标签

你也可以对过去的提交打标签。 假设提交历史是这样的:

$ 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

To tag that commit, you specify the commit checksum (or part of it) at the end of the command.
现在,假设在 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 <schacon@gee-mail.com>
Date:   Mon Feb 9 15:32:16 2009 -0800version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <mchacon@gee-mail.com>
Date:   Sun Apr 27 20:43:35 2008 -0700updated rakefile
...

4. Sharing Tags (共享标签)

默认情况下,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 git@github.com: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 git@github.com:schacon/simplegit.git* [new tag]         v1.4 -> v1.4* [new tag]         v1.4-lw -> v1.4-lw

git push 推送两种标签。使用 git push <remote> --tags 推送标签并不会区分轻量标签和附注标签, 没有简单的选项能够让你只选择推送一种标签。

5. Deleting Tags (删除标签)

要删除掉你本地仓库上的标签,可以使用命令 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 <remote> :refs/tags/<tagname>

$ git push origin :refs/tags/v1.4-lw
To /git@github.com:schacon/simplegit.git- [deleted]         v1.4-lw

The way to interpret the above is to read it as the null value before the colon is being pushed to the remote tag name, effectively deleting it.
上面这种操作的含义是,将冒号前面的空值推送到远程标签名,从而高效地删除它。

第二种更直观的删除远程标签的方式是:

$ git push origin --delete <tagname>

6. Checking out Tags (检出标签)

如果你想查看某个标签所指向的文件版本,可以使用 git checkout 命令,虽然这会使你的仓库处于分离头指针 (detached HEAD) 的状态,这个状态有些不好的副作用:

(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git checkout v2.8.2
Checking out files: 100% (12467/12467), done.
Note: checking out 'v2.8.2'.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 2ea19cbb575 Merge pull request #56213 from tensorflow/mm-disable-tests-on-r2.8
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git status
HEAD detached at v2.8.2
nothing to commit, working tree clean
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git branch
* (HEAD detached at v2.8.2)master
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$

在“分离头指针”状态下,如果你做了某些更改然后提交它们,标签不会发生变化,但你的新提交将不属于任何分支,并且将无法访问,除非通过确切的提交哈希才能访问。因此,如果你需要进行更改,比如你要修复旧版本中的错误,那么通常需要创建一个新分支:

(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git checkout -b v2.8.2 v2.8.2
Switched to a new branch 'v2.8.2'
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git branchmaster
* v2.8.2
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$ git status
On branch v2.8.2
nothing to commit, working tree clean
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$

如果在这之后又进行了一次提交,v2.8.2 分支就会因为这个改动向前移动,此时它就会和 v2.8.2 标签稍微有些不同。

tag 本身指向的就是一个 commit,所以同根据 commit id 检出分支是一个道理。如果我们想要修改 tag 检出代码分支,那么虽然分支中的代码改变了,但是 tag 标记的 commit 还是同一个,标记的代码是不会变的,这个要格外的注意。

References

https://yongqiang.blog.csdn.net/
https://book.git-scm.com/docs/gitglossary
https://git-scm.com/book/en/v2/Git-Basics-Tagging
https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE

git tag (打标签)相关推荐

  1. Git tag 打标签

    Git tag 给当前分支打标签 给指定的commit打Tag 打Tag不必要在head之上,也可在之前的版本上打,这需要你知道某个提交对象的校验和(通过git log获取). 补打Tag $ git ...

  2. 【Git】Git 标签使用 ( 查询哈希码 | 创建标签 git tag v1.0 | 查询标签 git tag | 查询标签信息 git show v1.0 | 创建标签并指定说明 | 删除标签 )

    文章目录 一.查询提交记录哈希码 1.git log --pretty=oneline --abbrev-commit 2.git reflog 二.为某个提交设置标签 git tag v1.0 23 ...

  3. git tag打标签常用命令

    # 创建轻量标签 $ git tag v0.1.2-light 切换到标签 与切换分支命令相同,用git checkout [tagname] 查看标签信息 用git show命令可以查看标签的版本信 ...

  4. git tag 打标签(我看过最透彻的文章)

    打标签 像其他版本控制系统(VCS)一样,Git 可以给仓库历史中的某一个提交打上标签,以示重要. 比较有代表性的是人们会使用这个功能来标记发布结点( v1.0 . v2.0 等等). 在本节中,你将 ...

  5. git branch分支创建、切换、合并,git tag标签

    一)Git分支简介 每一种版本控制系统都以某种形式支持分支.使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作. 列出分支命令:git branch 创建分支命令:git bra ...

  6. Git 之 git tag标签使用

    目录 一.简介 二.本地tag操作 1.创建tag标签 (1)创建轻量标签 (2)创建附注标签 2.查看tag标签 (1)查看标签列表 (2)查看标签提交信息 (3)在提交历史中查看标签 3.删除ta ...

  7. 使用git tag为代码仓库打标签

    在使用git进行项目管理时,一般使用指定特定的版本号或者分支上线部署.一般会有dev.test或release.master或pord分别表示开发.测试.上线分支,但是使用分支管理的时候,一般会从主分 ...

  8. git tag创建、远程推送、回退以及强推push -f

    一.给本地仓库分支打轻量级tag标签 1.在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branchdevwhdgray * masteroptimize_sel_drive ...

  9. git tag 操作

    说明 tag(标签) 是 git 版本库的指向某个 commit 的指针.主要用于在代码版本管理时,保存一个阶段性的版本. tag 对应某次 commit, 是一个点,是不可移动的.branch 对应 ...

  10. GIT中打标签(tag)的意义

    在git代码管理时,有时候我们想对某个特定的commit 添加标记,比如要标识版本信息,这时候就可以用的git中的打标签功能.打tag就类似于我们看书放书签一样,以后可以直接用tag找到提交的位置,不 ...

最新文章

  1. java中关键字volatile的作用(转载)
  2. Java程序设计进阶之路一:捕捉异常
  3. 关于ssh 配置文件的参数说明
  4. 华为p4用鸿蒙系统吗_华为p40pro是鸿蒙系统吗
  5. 信息学奥赛C++语言:判断奇偶
  6. 依赖项出现感叹号怎么办_「电脑常用技巧」不喜欢看到的黄色感叹号
  7. 记一次nodemanager无法启动的情况
  8. 三大框架搭建项目常见问题QA整理
  9. 项目人力资源管理之编制项目人力资源管理计划
  10. 博弈论 | 博弈论简谈、常见的博弈定律、巴什博弈
  11. 中国移动开放平台(dev.cmccopen.cn)请求头Header:Authorization验证失败的原因(我遇到的)
  12. 诗歌十 寒窑赋(破窑赋)天有不测风云,人有旦夕祸福
  13. 抓包PC微信小程序失败解决方法
  14. 说说大型网站可伸缩性架构的设计原理
  15. 【面试系列三】面试是面试者与面试官的双向沟通,如何抓住面试官的小尾巴以及面试过程中需要避开的一些减分项!
  16. m4a怎么转换成mp4?一个简单好用的操作教程分享
  17. 基于RTP协议的IP电话QoS监测及提高策略
  18. 核桃编程学python吗_西瓜编程和核桃编程哪个好
  19. C Primer Plus 第5章 运算符、表达式和语句 5.3 其他运算符
  20. windows10 安装

热门文章

  1. 打字时禁用触摸板_触摸打字感觉不错,但不适合我
  2. U盘PE启动盘制作好后,如何进入PE系统?
  3. U盘重装Mac全新的操作系统详细教程
  4. java 裁剪 pdf_PDFBox:使用Java轻松从PDF文件提取内容
  5. python统计字符串个数_python实现字符串中字符分类及个数统计
  6. 美团点评 2019校招 前端方向职位试卷在线考试
  7. 如何运用SWOT分析法
  8. CQF笔记M1L2二叉树模型
  9. 关于cups打印管理详细整理
  10. 程序员数学(29)–投影与视图