git tag

本文于2018年1月3号发布在个人博客中,因为个人博客关闭,全部迁移到CSDN,以下是正文:


随着时间的流逝,程序员一次又一次的往仓库中提交代码,直到又一次提交完成之后,项目经理宣布:“到此刻为止,我们1.0版本的开发完成了”

今天来说说git中跟版本发布相关的特性:git tag。本文将按照如下流程进行讲解:

  • 关于tag
  • 创建tag
  • 提交tag
  • 检出tag
  • 删除tag

关于tag

tag用来标记某个提交的重要性,可以用作版本发布的标记。当把tag push到github,可以在release中看到:

这里写图片描述

git中的tag有两种:

  • 轻量级tag(lightweight),“一个轻量标签很像一个不会改变的分支 – 它只是一个特定提交的引用”(引用自progit中文版)
  • 附注tag,“附注标签是存储在 Git 数据库中的一个完整对象。 它们是可以被校验的;其中包含打
    标签者的名字、电子邮件地址、日期时间;还有一个标签信息;并且可以使用 GNU Privacy
    Guard (GPG)签名与验证”(引用自progit中文版)

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

创建tag

不管是轻量级tag还是附注tag,都可以基于当前commit,或者指定某个commit打标签。

基于当前commit:

轻量级tag: git tag v1.1-lw
附注tag: git tag -a v1.0 -m "before aws test"

基于指定commit:

轻量级tag: git tag v1.1-lw ${commit-SHA-1}
附注tag: git tag -a -m "before aws test" v1.0 ${commit-SHA-1}

轻量级tag

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

$ git tag v1.1-lw 3a0b882ff42121f72f2490d9c1eced342cbcdef0
$ git tag
v1.0
v1.1-lw
$ git show v1.1-lw
commit 3a0b882ff42121f72f2490d9c1eced342cbcdef0 (tag: v1.1-lw)
Author: anyscoding <34288556+anyscoding@users.noreply.github.com>
Date: Tue Dec 12 11:51:57 2017 +0800

delete lines for aws test

diff --git a/README.md b/README.md
index 5d7e7d1..3f5c828 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,2 @@
 # HelloWorld
 this is my first project on github
-
-add new line for aws test build action output
-
-add new line for aws test build action output

可以看出,git show 轻量级标签,标签所在的commit的信息

附注tag

$ git tag -a -m "before aws test" v1.0 cce0567986c702792a28ff510fef6c585e68344f 
$ git tag
v1.0
v1.1-lw

$ git show v1.0
tag v1.0
Tagger: anyscoding <anyscoding@sina.com>
Date: Wed Jan 3 07:27:58 2018 +0800

before aws test

commit cce0567986c702792a28ff510fef6c585e68344f (tag: v1.0)
Author: anyscoding <34288556+anyscoding@users.noreply.github.com>
Date: Tue Dec 12 10:28:14 2017 +0800

Update README.md

diff --git a/README.md b/README.md
index 64ee367..417cbd9 100644
--- a/README.md
+++ b/README.md
@@ -2,3 +2,5 @@
 this is my first project on github

add new line
+
+add new line for aws codepipeline

当使用git show查看一个附注tag时,除了显示打标签的commit信息外,还会显示tag本身的信息,包括:tag名称、打tag的人、打tag的时间

提交tag

本地打好的tag,要如何共享出去呢?

git push origin v1.0
Counting objects: 1, done.
Writing objects: 100% (1/1), 159 bytes | 159.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:anyscoding/HelloWorld.git
 * [new tag] v1.0 -> v1.0

如果本地打了多个tag,想要一次性全部共享出去:

$ git tag
 v1.0
 v1.1
 v1.2

$ git push origin --tags
 Counting objects: 2, done.
 Delta compression using up to 4 threads.
 Compressing objects: 100% (2/2), done.
 Writing objects: 100% (2/2), 273 bytes | 273.00 KiB/s, done.
 Total 2 (delta 0), reused 0 (delta 0)
 To github.com:anyscoding/HelloWorld.git
 * [new tag] v1.1 -> v1.1
 * [new tag] v1.2 -> v1.2

在github上查看:

这里写图片描述

检出tag

如果想要查看打tag的状态,常见的场景是:从某个tag重新出发布包,可以通过如下命令:

git checkou ${tag-name}

$ git checkout v1.0
Note: checking out 'v1.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-name>

HEAD is now at cce0567... Update README.md

an@DESKTOP-IEU7HQD MINGW64 /d/GitHub/HelloWorld ((v1.0))

删除tag

tag创建错误,或者由于其他什么原因,需要删除某个tag。包括删除本地tag和删除远程tag

删除本地tag

git tag -d ${tag-name}
$ git tag -d v1.1
Deleted tag 'v1.1' (was c67a1ab)

删除远程tag

git push origin :refs/tags/${tag-name}
$ git push origin :refs/tags/v1.1
To github.com:anyscoding/HelloWorld.git
 - [deleted] v1.1

再看看github上:

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值