Git | 标签操作

概述

  • 作用 ------ 为重要版本(如发布版本)创建易记的名称(如 v1.2),替代难记的 commit id,类似 IP 和域名的关系

  • 本质 ------ 指向特定 commit 的不可变指针(与分支不同,分支会随提交移动)

    标签总是和某个 commit 挂钩。如果这个 commit 既出现在 master 分支,又出现在 dev 分支,那么两个分支上都可以看到这个标签

  • 特点

    • 标签与 commit 绑定,若 commit 存在于多个分支,标签在这些分支均可见
    • 标签内容包含标签名、关联的 commit、创建者、时间及可选说明信息
  • 使用场景

    • 版本发布:为稳定版本打标签(如 v1.0.0),便于后续回滚或发布
    • 代码审查:标记关键节点(如 feature-complete
    • 协作规范
      • 使用语义化版本(SemVer):主版本.次版本.修订号(如 v2.1.3
      • 附注标签推荐:包含版本变更摘要(git tag -a v1.1 -m "新增用户登录功能"
    • 与分支对比
      • 分支用于日常开发(动态移动),标签用于标记里程碑(静态快照)

指令介绍

标签管理 git tag
  • 作用 ------ 管理标签,包括创建、列出、删除或验证等

  • 语法

    bash 复制代码
    git tag [<options>] <tagname> [<commit>]
    参数 options 说明
    -a / --annotate 创建附注标签 annotated tag,包含作者、日期和描述信息
    -m <msg> / --message=<msg> 指定标签的描述信息(必须与 -a 一起使用)
    -d 删除指定标签
    -l / --list 列出符合模式的标签(如 git tag -l "v1.*"
    -s / --sign 创建 GPG 签名标签(需配置 GPG 密钥)
  • 具体示例

    • 列出所有标签

      shell 复制代码
      $ git tag
    • 创建轻量标签(无额外信息)

      shell 复制代码
      $ git tag v1.0
    • 对历史提交打标签

      shell 复制代码
      $ git log --oneline             # 查看 commit id
      $ git tag v0.9 a1b2c3d          # 对 commit a1b2c3d 打标签
    • 创建附注标签(含描述)

      shell 复制代码
      $ git tag -a v1.1 -m "正式发布版本"
    • 删除本地标签

      shell 复制代码
      $ git tag -d v0.9
    • 推送标签到远程

      shell 复制代码
      $ git push origin v1.0
      $ git push origin --tags        # 推送所有未推送的标签
    • 删除远程标签

      shell 复制代码
      $ git tag -d v1.0
      $ git push origin :refs/tags/v1.0
查看对象详情 git show
  • 作用 ------ 显示 Git 对象(如标签、提交、文件)详细信息,包括变更内容、作者、时间等

  • 语法

    bash 复制代码
    git show [<options>] [<object>]
    参数 options 说明
    --pretty=format:"..." 自定义输出格式(如 %H 显示完整 commit hash
    --name-only 仅显示受影响的文件名,不显示具体变更
    --stat 显示简略统计信息(变更行数)
  • 具体示例

    • 查看标签信息(含关联的提交和变更)

      shell 复制代码
      $ git show v1.0
      tag v1.0
      Tagger: Alice <alice@example.com>
      Date:   Mon Jan 1 12:00:00 2024 +0800
      
      Version 1.0 release
      
      commit a1b2c3d...
      Author: Bob <bob@example.com>
      Date:   Sun Dec 31 12:00:00 2023 +0800
      
          feat: Add new feature
      
      diff --git a/file.txt b/file.txt
      ...
    • 查看某次提交的变更

      shell 复制代码
      $ git show abc1234
    • 查看分支最新提交

      shell 复制代码
      $ git show main

本地标签操作

场景一:查看标签
  • 查看所有标签

    shell 复制代码
    $ git tag
    v0.9
    v1.0

    注意:标签不是按时间顺序列出,而是按字母排序的

  • 查看标签信息

    shell 复制代码
    $ git show v0.9
    commit f52c63349bc3c15dfasf972b82c8f286 (tag: v0.9)
    Author: Li Hua <example@mail.com>
    Date:   Fri May 23 09:07:00 2025 +0800
    
        添加合并功能
    
    diff --git a/readme.txt b/readme.txt
    ...
场景二:当前提交创建标签 v1.0
  1. 切换分支 ------ 切换到要打标签的分支

    shell 复制代码
    $ git branch
    * dev
      master
    shell 复制代码
    $ git checkout master
    Switched to branch 'master'
  2. 创建标签 ------ 当前提交打新标签 v1.0

    shell 复制代码
    $ git tag v1.0
场景三:历史提交创建标签 v1.0
  1. 查找提交 ------ 找到历史提交的 commit id

    shell 复制代码
    $ git log --pretty=oneline --abbrev-commit
    12a631b (HEAD -> master) 合并bug修复
    f52c633 添加合并功能
    cf810e4 解决冲突
  2. 创建标签 ------ 历史提交打新标签 v1.0

    shell 复制代码
    $ git tag v0.9 f52c633
场景三:创建带有说明的标签
  1. 创建带说明标签 ------ 说明中,-a 指定标签名,-m 指定说明文字,最后加上 commit id

    shell 复制代码
    $ git tag -a v0.1 -m "version 0.1 released" cf810e4
  2. 查看标签信息

    shell 复制代码
    $ git show v0.1
    tag v0.1
    Tagger: Li Hua <example@mail.com>
    Date:   Fri May 23 09:07:00 2025 +0800
    
    version 0.1 released
    
    commit cf810e49bc3sad972b82c8f286 (tag: v0.1)
    Author: Li Hua <example@mail.com>
    Date:   Fri May 23 09:07:00 2025 +0800
    
        解决冲突
    
    diff --git a/readme.txt b/readme.txt
    ...
场景四:删除本地标签
  • 删除标签 ------ 删除指定名称标签

    sh 复制代码
    $ git tag -d v0.1
    Deleted tag 'v0.1' (was cf810e4)

远程标签操作

!important

标签不会自动同步到远程,需要显式推送

场景一:推送某个本地标签到远程
  1. 推送指定名称标签

    sh 复制代码
    $ git push origin v1.0
    Total 0 (delta 0), reused 0 (delta 0)
    To github.com:michaelliao/learngit.git
     * [new tag]         v1.0 -> v1.0
场景二:推送全部本地标签到远程
  1. 推送所有标签 ------ 采用指令 git push--tags 参数推送所有标签

    sh 复制代码
    $ git push origin --tags
    Total 0 (delta 0), reused 0 (delta 0)
    To github.com:michaelliao/learngit.git
     * [new tag]         v0.9 -> v0.9
场景三:删除远程标签
  1. 本地删除标签

    shell 复制代码
    $ git tag -d v0.9
    Deleted tag 'v0.9' (was f52c633)
  2. 远程删除标签

    shell 复制代码
    $ git push origin :refs/tags/v0.9
    To github.com:michaelliao/learngit.git
     - [deleted]         v0.9

相关推荐
PaQiuQiu1 天前
GitHub 开源分享 | Coding Interview University
面试·开源·github
毛毛蹭蹭1 天前
github copilot 0.33模型使用问题
github
介一安全1 天前
国内 GitHub 仓库下载提速
gitee·github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-01-17)
ai·开源·大模型·github·ai教程
CryptoRzz1 天前
印度股票数据API对接实战(实时行情与IPO功能全解析)
websocket·区块链·github·共识算法·分布式账本
蜜汁小强1 天前
macOS 上的git代理配置在哪里
git·macos·代理模式·proxy模式
钟佩颖1 天前
Git .
git
Aliex_git1 天前
GitHub Copilot 使用笔记
笔记·学习·github·copilot·ai编程
时光慢煮1 天前
Flutter 编译开发 OpenHarmony 全流程实战教程-基于开源仓库GitCode 搜索工具 v1.0.3 的跨平台实践
flutter·开源·gitcode
CoderJia程序员甲2 天前
GitHub 热榜项目 - 日榜(2026-1-12)
ai·开源·大模型·github·ai教程