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

相关推荐
nyf_unknown6 小时前
(vue)将文件夹打成tar包, Git Bash(推荐)具体使用
vue.js·git·bash
兔老大RabbitMQ6 小时前
Git Revert 特定文件/路径的方法
git
星哥说事1 天前
如何将堡塔云WAF迁移到新的服务器
服务器·git·github
阿政一号1 天前
Git版本控制器
git
妮妮喔妮1 天前
SSH协议的GIT转换
运维·git·ssh
今禾1 天前
Git 日常使用与面试考点详解:从入门到精通
前端·git·面试
Data_Adventure2 天前
能连上 GitHub(SSH 验证成功),却 push 失败?常见原因与逐步解决方案
前端·git·github
间彧2 天前
如何解决Git客户端下载缓慢问题
git
Tearstornbyrain2 天前
在Ubuntu24.04中使用ssh连接本地git仓库到github远程仓库
linux·git·ubuntu·ssh·github
四七伵2 天前
一次 Git Rebase 事故,让我彻底明白 Rebase 和 Merge 的区别
git·后端