Git标签管理(对版本打标签,起别名)

tag

理解标签

标签 tag ,可以简单的理解为是对某次 commit 的⼀个标识,相当于起了⼀个别名。

例如,在项⽬发布某个版本的时候,针对最后⼀次 commit 起⼀个 v1.0 这样的标签来标识⾥程碑的意义。

相较于难以记住的 commit id , tag 很好的解决这个问题,当我们需要回退到某个重要版本时,直接使⽤标签就能很快定位到。

创建标签

git tag [name]

在Git中打标签⾮常简单,⾸先,切换到需要打标签的分⽀上:

csharp 复制代码
[root@VM-16-15-centos ~]# git branch
* dev1
  master

然后,敲命令 git tag [name] 就可以打⼀个新标签:

csharp 复制代码
[root@VM-16-15-centos ~]# git tag v.1.0

可以⽤命令 git tag 查看所有标签:

csharp 复制代码
[root@VM-16-15-centos ~]# git tag
v.1.0

默认标签是打在最新提交的 commit 上的。那如何在指定的commit上打标签呢?⽅法是找到历史提交的commit id,然后打上就可以了 ,⽰例如下:

csharp 复制代码
[root@VM-16-15-centos ~]# git log --pretty=oneline --abbrev-commit
36d40bc rm readme
f2e6815 alter version3
6023a0f alter version2
3e0ccde alter version1
6e4b54f readme alter 1
bcfa533 add file2
937c2af add file
f1e6436 commit 3 files
3e65956 commit my first fle
[root@VM-16-15-centos ~]# git tag v.2.0 3e65956

我们对"commit my first file"这次提交进行了打标签。

git show [tagname]

可以⽤ git show [tagname] 查看标签信息。

csharp 复制代码
[root@VM-16-15-centos ~]# git show v.2.0
commit 3e6595623aeef2d29588d0f6d0fb359d40307e75
Author: zjl <123456.com>
Date:   Sun Jul 16 18:38:59 2023 +0800

    commit my first fle

diff --git a/gitcode/readme b/gitcode/readme
new file mode 100644
index 0000000..3b18e51
--- /dev/null
+++ b/gitcode/readme
@@ -0,0 +1 @@
+hello world

Git还提供可以创建带有说明的标签,⽤-a指定标签名,-m指定说明⽂字,格式为:

csharp 复制代码
git tag -a [name] -m "XXX" [commit_id]

操作标签

删除标签

git tag -d < tagname >

如果标签打错了,也可以删除:

csharp 复制代码
[root@VM-16-15-centos ~]# git tag
v.1.0
v.2.0
[root@VM-16-15-centos ~]# git tag -d v.1.0
Deleted tag 'v.1.0' (was 36d40bc)
[root@VM-16-15-centos ~]# git tag
v.2.0

因为创建的标签都只存储在本地,不会⾃动推送到远程。所以,打错的标签可以在本地安全删除。

推送某个标签到远程

git push origin < tagname >

如果要推送某个标签到远程,使⽤命令 git push origin <tagname>

csharp 复制代码
[root@VM-16-15-centos git_study]# git push origin v.1.0
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:ZMZZZhao/git_study.git
 * [new tag]         v.1.0 -> v.1.0

此时,查看远端码云,看到了标签已经被更新!

当然,如果你本地有很多标签,也可以⼀次性的全部推送到远端:

csharp 复制代码
git push origin --tags

如果标签已经推送到远程,要删除远程标签就⿇烦⼀点,先从本地删除:

csharp 复制代码
[root@VM-16-15-centos git_study]# git tag -d v.1.0
Deleted tag 'v.1.0' (was 11842c3)

然后,从远程删除。删除命令也是push,但是格式如下:

csharp 复制代码
[root@VM-16-15-centos git_study]# git push origin :refs/tags/v.1.0
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:ZMZZZhao/git_study.git
 - [deleted]         v.1.0

此时远端的标签就被删除了。

相关推荐
旅者时光5 小时前
Git使用基础
git
Clownorange5 小时前
git安装和配置
git
网安2311 015 小时前
OWASP ZAP 安全工具深度剖析:从环境搭建到架构复原的结对编程实践
git
ShineWinsu8 小时前
对于Linux:git版本控制器和cgdb调试器的解析
linux·c语言·git·gitee·github·调试·cgdb
php_kevlin10 小时前
git提交限制规范
大数据·git·elasticsearch
安大小万10 小时前
Git 常用命令终极指南:从入门到进阶
git
摇滚侠10 小时前
GIT 代码冲突 git pull 和 git pull rebase 的区别,保持提交记录的线性整齐
git
vistaup1 天前
windows git 更新当前目录下所有的文件(非递归)
windows·git
王码码20351 天前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
Irene19911 天前
Git 命令汇总表(基于一次完整的 Git 实战经验整理,涵盖从安装配置到日常开发、问题排查的所有常用命令)
git·常用命令