【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 使用等),可继续提问。

相关推荐
不爱学英文的码字机器1 天前
Git误操作急救手册大纲
git
A懿轩A1 天前
【2026 最新】Mac 电脑配置指南:Homebrew 安装换源 + Git 环境配置(保姆级教程)
git
切糕师学AI1 天前
Visual Studio Git 使用指南
git·elasticsearch·visual studio
蜜汁小强1 天前
Git Worktree:在不打断当前开发的情况下紧急修复生产问题
git
宇宙realman_9991 天前
Git 本地版本控制极简使用笔记(Qt 项目专用)
笔记·git
坐吃山猪1 天前
OpenClaw02_GitHook使用
git·hook·openclaw
莫寒清1 天前
Git分支命名规范与最佳实践
git
无限进步_1 天前
深入解析C++容器适配器:stack、queue与deque的实现与应用
linux·开发语言·c++·windows·git·github·visual studio
程序员爱酸奶2 天前
Git + 云原生:构建坚如磐石的 Kubernetes 配置版本管理
git·云原生·kubernetes
Liu.7742 天前
vscode使用git和svn
git·vscode·svn