赞
踩
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 等等)。
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 要向前开发。
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 的分支,并成为活动分支。在大多数情况下,这包含本地开发,尽管这纯粹是按照惯例并且不是必需的。
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 分支,开发人员使用它来识别开发的概念线。由于分支非常简单且成本低廉,因此通常希望有几个小分支,每个分支都包含定义非常明确的概念或小的增量但相关的更改。
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 指向该分支。
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 中的特定点。
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) 签名,在这种情况下,它被称为签名标签对象。
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
就是强制使用的。
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) 签名并验证。通常会建议创建附注标签,这样你可以拥有以上所有信息。但是如果你只是想用一个临时的标签,或者因为某些原因不想要保存这些信息,那么也可以用轻量标签。
在 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 -0700
Merge pull request #56213 from tensorflow/mm-disable-tests-on-r2.8
Disable flaky tests
(base) yongqiang@yongqiang:~/tensorflow_work/tensorflow_tag_v2.8.2/tensorflow$
输出显示了打标签者的信息、打标签的日期时间、附注信息,然后显示具体的提交信息。
另一种给提交打标签的方式是使用轻量标签。轻量标签本质上是将提交校验和存储到一个文件中,没有保存任何其他信息。创建轻量标签,不需要使用 -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 -0700
changed the version number
你也可以对过去的提交打标签。 假设提交历史是这样的:
$ 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 -0800
version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <mchacon@gee-mail.com>
Date: Sun Apr 27 20:43:35 2008 -0700
updated rakefile
...
默认情况下,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
推送标签并不会区分轻量标签和附注标签, 没有简单的选项能够让你只选择推送一种标签。
要删除掉你本地仓库上的标签,可以使用命令 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>
如果你想查看某个标签所指向的文件版本,可以使用 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 branch
master
* 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 还是同一个,标记的代码是不会变的,这个要格外的注意。
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。