Git标签管理

一 Git有两种标签类型

  1. 轻量标签(Lightweight Tag):只是一个指向特定提交的指针
  2. 附注标签(Annotated Tag):包含更多元数据(如标签作者、日期、标签消息)
类型 命令 说明 适用场景
轻量标签 git tag v1.0 仅指向提交 简单标记,无需额外信息
附注标签 git tag -a v1.0 -m "Release version 1.0" 包含作者、日期、消息 重要版本,需要详细说明

关键点:

  1. 附注标签是推荐使用的,因为它包含更多信息
  2. 标签是固定的,不会随着新提交而移动
  3. 标签可以推送至远程仓库,方便团队共享

小贴士:在GitCode上,附注标签会显示在仓库的"标签"页面,而轻量标签则不会。

二 步骤1:创建本地标签

shell 复制代码
# 1. 创建测试项目
mkdir git-tag-demo && cd git-tag-demo
git init
echo "# Git Tag Demo" > README.md
git add README.md
git commit -m "Initial commit"

# 2. 创建轻量标签
git tag v1.0

# 3. 创建附注标签(推荐)
git tag -a v1.0 -m "First stable release"

三 步骤2:查看标签

shell 复制代码
# 查看所有标签
git tag

# 查看标签详情(附注标签)
git show v1.0

执行结果示例:

shell 复制代码
tag v1.0
Tagger: Your Name <your.email@example.com>
Date:   Mon Jan 1 12:00:00 2024 +0800

First stable release

四 步骤3:推送标签到GitCode

shell 复制代码
# 添加远程仓库
git remote add origin https://gitcode.com/your-username/git-tag-demo.git
# 推送标签(单个标签)
git push origin v1.0
# 推送所有标签
git push origin --tags

重要提示:默认情况下,git push不会推送标签,需要显式指定--tags。

五 删除标签

原因:需要删除本地和远程的标签。

解决:

复制代码
# 删除本地标签
git tag -d v1.0
# 删除远程标签
git push origin --delete v1.0

六 标签与分支混淆

原因:标签和分支看起来很相似。

解决:

复制代码
标签:git tag查看,通常以v开头(如v1.0)
分支:git branch查看,通常以feature/开头(如feature/login)
相关推荐
悟空瞎说20 小时前
Git Worktree 实战:多 AI 编码代理并行开发,彻底解决分支切换冲突痛点
前端·git
BING_Algorithm1 天前
开发常用Git核心知识
git·后端
Lyyaoo.1 天前
Git常用命令及应用
git
呆萌很1 天前
Git 配置 .gitignore 文件
git
小则又沐风a1 天前
Linux下的Git的上传(版本控制器)
linux·数据库·git
天真吴邪xie1 天前
Claude Code安装
java·git
曾几何时`2 天前
虚拟环境pip
git
她说可以呀2 天前
git的版本回退
git