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

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

相关推荐
课堂随想10 分钟前
SHA-1 是一种不可逆的、固定长度的哈希函数,在 Git 等场景用于生成唯一的标识符来管理对象和数据完整性
git·哈希算法
易雪寒3 小时前
IDEA在git提交时添加忽略文件
java·git·intellij-idea
徒步僧4 小时前
mac中文件夹怎么显示.git隐藏文件
git·macos
int WINGsssss1 天前
Git使用
git
用户0760530354381 天前
Git Revert:安全移除错误提交的方式
git
Good_Starry2 天前
Git介绍--github/gitee/gitlab使用
git·gitee·gitlab·github
云端奇趣2 天前
探索 3 个有趣的 GitHub 学习资源库
经验分享·git·学习·github
F_D_Z2 天前
【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem:
网络·git·网络协议·ssl
等风来不如迎风去2 天前
【git】main|REBASE 2/6
git
艾伦~耶格尔2 天前
IDEA 配置 Git 详解
java·ide·git·后端·intellij-idea