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

相关推荐
222you7 小时前
git的命令
git
Coolbike8 小时前
Git工作流
git
~央千澈~12 小时前
实战针对本地项目git如何移除旧仓库关联并且添加关联新仓库-优雅草卓伊凡
git
JH307318 小时前
git常用命令大全
git
ll5776443321 天前
使用PyTorch实现自定义损失函数以FocalLoss为例的详细教程
git
李少兄1 天前
Git 用户名与邮箱配置指南
git
Blue啊1 天前
code Merge(qcc)
git·gitlab
洛小豆1 天前
Git打标签仓库看不到?她说:豆子,你又忘了加 --tags!
git·后端·github
好奇的菜鸟1 天前
理解 Git 命令 `git reset --hard origin/pre`:版本回退的“利刃”与使用禁忌
大数据·git
春生野草1 天前
git的使用
git