Git 打标签

前言

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

1、列出标签

  • 在 Git 中列出已有的标签是非常简单直观的。只需要输入 git tag,这个命令以字母顺序列出标签,但是它们出现的顺序并不重要。

    1
    $ git tag
    1
    2
    v0.1
    v1.3
  • 你也可以使用特定的模式查找标签。

    • 例如,Git 自身的源代码仓库包含标签的数量超过 500 个。如果只对 1.8.5 系列感兴趣,可以运行。

      1
      2
      # git tag -l [特定的模式]
      $ git tag -l 'v1.8.5*'
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      v1.8.5
      v1.8.5-rc0
      v1.8.5-rc1
      v1.8.5-rc2
      v1.8.5-rc3
      v1.8.5.1
      v1.8.5.2
      v1.8.5.3
      v1.8.5.4
      v1.8.5.5

2、创建附注标签

  • Git 使用两种主要类型的标签:轻量标签(lightweight)与附注标签(annotated)。

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

  • 通常建议创建附注标签,这样你可以拥有以上所有信息;但是如果你只是想用一个临时的标签,或者因为某些原因不想要保存那些信息,轻量标签也是可用的。

  • 在 Git 中创建一个附注标签是很简单的。最简单的方式是当你在运行 tag 命令时指定 -a 选项。

    1
    2
    # git tag -a [标签名] -m [标签说明]
    $ git tag -a v1.4 -m 'my version 1.4'
    1
    2
    3
    4
    $ git tag
    v0.1
    v1.3
    v1.4
    • -m 选项指定了一条将会存储在标签中的信息。如果没有为附注标签指定一条信息,Git 会运行编辑器要求你输入信息。
  • 这时,如果在标签上运行 git show,你会看到显示了打标签者的信息、打标签的日期时间、附注信息,然后显示具体的提交信息。

3、创建轻量标签

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

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

    1
    2
    # git tag [标签名]-lw
    $ git tag v1.4-lw
    1
    2
    3
    4
    5
    6
    $ git tag
    v0.1
    v1.3
    v1.4
    v1.4-lw
    v1.5
  • 这时,如果在标签上运行 git show,你不会看到额外的标签信息,命令只会显示出具体的提交信息。

4、显示标签信息

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

    • 附注标签会显示打标签者的信息、打标签的日期时间、附注信息,然后显示具体的提交信息
    • 轻量标签会只会显示出具体的提交信息。

      1
      2
      # git show [标签名]
      $ git show v1.4
    • 附注标签

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      tag v1.4
      Tagger: Ben Straub <ben@straub.cc>
      Date: Sat May 3 20:19:12 2014 -0700

      my version 1.4

      commit ca82a6dff817ec66f44342007202690a93763949
      Author: Scott Chacon <schacon@gee-mail.com>
      Date: Mon Mar 17 21:52:11 2008 -0700

      changed the version number
    • 轻量标签

      1
      2
      3
      4
      5
      6
      $ 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

5、后期打标签

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ 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” 提交。你可以在之后补上标签。要在那个提交上打标签,你需要在命令的末尾指定提交的校验和(或部分校验和)。

    1
    2
    # git tag -a [标签名] [校验和] -m [标签说明]
    $ git tag -a v1.2 9fceb02 -m "version 1.2"
    • 可以看到你已经在那次提交上打上标签了。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      $ 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
      ...

6、共享标签

  • 默认情况下,git push 命令并不会传送标签到远程仓库服务器上。在创建完标签后你必须显式地推送标签到共享服务器上。这个过程就像共享远程分支一样,你可以运行 git push origin [tagname]

    1
    2
    # git push [branch-name] [tagname]
    $ git push origin v1.5
    1
    2
    3
    4
    5
    6
    7
    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 命令。这将会把所有不在远程仓库服务器上的标签全部传送到那里。

    1
    2
    # git push [branch-name] --tags
    $ git push origin --tags
    1
    2
    3
    4
    5
    6
    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
    • 现在,当其他人从仓库中克隆或拉取,他们也能得到你的那些标签。

7、检出标签

  • 在 Git 中你并不能真的检出一个标签,因为它们并不能像分支一样来回移动。如果你想要工作目录与仓库中特定的标签版本完全一样,可以使用 git checkout -b [branch-name] [tagname] 在特定的标签上创建一个新分支。

    1
    2
    # git checkout -b [branch-name] [tagname]
    $ git checkout -b version2 v2.0.0
    1
    Switched to a new branch 'version2'
    • 当然,如果在这之后又进行了一次提交,version2 分支会因为改动向前移动了,那么 version2 分支就会和 v2.0.0 标签稍微有些不同,这时就应该当心了。
文章目录
  1. 1. 前言
  2. 2. 1、列出标签
  3. 3. 2、创建附注标签
  4. 4. 3、创建轻量标签
  5. 5. 4、显示标签信息
  6. 6. 5、后期打标签
  7. 7. 6、共享标签
  8. 8. 7、检出标签
隐藏目录