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

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

相关推荐
自动花钱机2 小时前
Cherry-pick冲突与Git回滚
git
coderklaus8 小时前
git rebase
git
苏元8 小时前
☠️ 写错 commit = 绩效自爆,别说我没提醒你!
git
程序设计实验室8 小时前
模型文件硬塞进 Git,GitHub 直接打回原形:使用Git-LFS管理大文件
git
Dontla1 天前
脚本:git push直到成功(windows powershell命令)(Github连不上、Github断开)
git·github
CAE虚拟与现实1 天前
GitHub Desktop 和 Git 命令行工具(CLI)各有优势
git·github·github desktop
RePeaT1 天前
代码双仓库备份指南:三种简单高效的方法
git·github
coderklaus1 天前
Git GC
git
xiezhr1 天前
Git提交错了,别慌!还有后悔药
git·gitlab·github
GGGGGGGGGGGGGG.1 天前
CI/CD 全链路实践:从 Git 基础到 Jenkins + GitLab 企业级部署
运维·git·ci/cd·云原生·gitlab·jenkins