【Git】面对发布或重要节点,Git如何打Tag?

在 Git 中,tag 通常用于标记某个提交(commit)作为项目的某个重要节点,例如发布版本(v1.0、v2.0 等)。Git 支持两种类型的 tag:

  • 轻量标签(Lightweight):只是一个指向特定提交的指针。
  • 附注标签(Annotated):包含更多信息(如作者、时间、说明等),推荐使用。

🧾 常用 Git 打 Tag 操作

1. 查看已有标签

bash

git tag

2. 创建轻量标签

bash

git tag <tag_name>

示例:

bash

git tag v1.0-light

3. 创建附注标签(推荐)

bash

git tag -a <tag_name> -m "<message>"

示例:

bash

git tag -a v1.0 -m "Release version 1.0"

4. 给历史提交打标签

先查看提交历史获取 commit hash:

bash

git log --oneline

然后给指定 commit 打标签:

bash

git tag -a <tag_name> <commit_hash>

示例:

bash

git tag -a v0.9 abc1234

5. 推送标签到远程仓库

默认 git push 不会推送 tag,需手动推送:

bash

git push origin <tag_name>

推送所有本地标签:

bash

git push origin --tags

6. 删除本地标签

bash

git tag -d <tag_name>

7. 删除远程标签

bash

git push origin :refs/tags/<tag_name>

或使用新语法(Git >= v1.8.0):

bash

git push --delete origin <tag_name>

8. 查看某个标签详情

bash

git show <tag_name>


✅ 示例流程(创建并推送一个附注标签)

复制代码
# 创建附注标签
git tag -a v1.1 -m "正式发布1.1版本"

# 查看标签信息
git show v1.1

# 推送标签到远程仓库
git push origin v1.1

📌 小提示

  • 标签命名建议统一格式,如:v1.0.0release-202406
  • 在 CI/CD 或构建系统中,常通过 tag 触发自动打包、部署等流程。
  • 如果你正在处理的是团队项目,请确保 tag 的命名和用途清晰明确,避免冲突。

如有更具体场景(如从图形界面工具操作、配合 GitHub/Gitee 使用等),可继续提问。

相关推荐
vibecoding日记2 小时前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记12 小时前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger1 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP2 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思2 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享2 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立3 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003963 天前
git切换当前分支到远程分支
git
ON10N3 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code