Skip to content

git 标签

Marking 发布使用 git 标签 — lightweight and annotated 标签 for version management.

你将学到

  • 如何 创建 lightweight and annotated 标签
  • 为什么 annotated 标签是 preferred for 发布
  • Listing, pushing, and 删除标签
  • 语义 versioning 约定 for 标签

概念讲解

What git tag Does

git 标签 marks a specific commit with a named pointer. Tags are commonly used to mark release versions — v1.0.0, v1.1.0, v2.0.0 — so you can easily find and checkout a specific release.

git tag v1.0.0

Unlike branches, tags do not move. They point to one commit forever, making them perfect for marking significant points in history like releases.

Lightweight vs Annotated Tags

There are two types of tags:

Lightweight Tags

git tag v1.0.0

A lightweight tag is just a named pointer to a commit — like a bookmark. It stores no metadata (no author, date, or message).

Annotated Tags

git tag -a v1.0.0 -m "发布 1.0.0: 初始公共发布"

An annotated tag is a full Git object with:

  • A tag message
  • The tagger's name and email
  • A timestamp
  • A pointer to the commit

Always use annotated tags for releases. They store metadata and can be signed with GPG for verification.

Creating Tags

# Lightweight tag on the current commit
git tag v1.0.0

# Annotated tag on the current commit
git tag -a v1.0.0 -m "初始发布"

# Tag a specific commit (not the current one)
git tag -a v1.0.0 -m "初始发布" abc1234

# Signed tag (requires GPG key setup)
git tag -s v1.0.0 -m "Signed 发布"

Listing and Viewing Tags

git tag                    # list all tags
git tag -l "v1.*"          # list tags matching a pattern
git tag -l --sort=-v:refname  # sort by version (newest first)

git show v1.0.0            # see the tag details and the commit it points to

Pushing Tags

Tags are NOT pushed by default with git 推送. You must push them explicitly:

git push origin v1.0.0     # push a specific tag
git push origin --tags     # push all tags
git push --follow-tags     # push commits and any annotated tags

To always push annotated tags with commits:

git config --global push.followTags true

Deleting Tags

git tag -d v1.0.0              # delete a local tag
git push origin --delete v1.0.0  # delete a remote tag

Semantic Versioning

Tags typically follow Semantic Versioning (SemVer): MAJOR.MINOR.PATCH

  • MAJOR — incompatible API changes (1.0.0 → 2.0.0)
  • MINOR — new features, backward-compatible (1.0.0 → 1.1.0)
  • PATCH — bug fixes, backward-compatible (1.0.0 → 1.0.1)
git tag -a v1.0.0 -m "初始发布"
git tag -a v1.0.1 -m "Fix 登录 bug"
git tag -a v1.1.0 -m "添加搜索 feature"
git tag -a v2.0.0 -m "Breaking: 新 API 设计"

Pre-release versions use a hyphen: v2.0.0-beta.1, v2.0.0-rc.1.

Checking Out a Tag

To view the code at a specific release:

git checkout v1.0.0
# You are now in detached HEAD at the v1.0.0 release

# To make changes from a tag, create a branch:
git checkout -b hotfix-1.0.1 v1.0.0

GitHub Releases

On GitHub, tags can be turned into Releases — a web page with release notes, downloadable assets, and a tag. You can create releases via the GitHub website or CLI:

gh release create v1.0.0 --title "发布 1.0.0" --notes "初始发布"
gh release create v1.0.0 --generate-notes  # auto-generate from PRs

Releases are the public face of your tags — they are what users see on your GitHub repo's "发布" page.

示例代码

              # --- Create an annotated tag for a release ---
git tag -a v1.0.0 -m "Initial public release"
git tag -a v1.0.1 -m "Fix: resolve crash on empty cart"
git tag -a v1.1.0 -m "Feature: add dark mode"
git tag -a v2.0.0 -m "Breaking: redesign API"

# --- List and inspect tags ---
git tag
# v1.0.0
# v1.0.1
# v1.1.0
# v2.0.0

git tag -l "v1.*"        # list only v1.x tags
git show v1.0.0          # see tag details + commit

# --- Push tags to remote ---
git push origin v1.0.0           # push one tag
git push origin --tags           # push all tags

# --- Tag a specific past commit ---
git log --oneline
# abc1234 feat: initial commit
git tag -a v0.1.0 -m "Pre-release" abc1234

# --- Checkout a release ---
git checkout v1.0.0
# (detached HEAD — just looking)
git checkout main  # go back

# --- Create a hotfix branch from a tag ---
git checkout -b hotfix-1.0.2 v1.0.1

# --- Delete a tag ---
git tag -d v0.1.0                  # delete locally
git push origin --delete v0.1.0    # delete on remote

# --- Create a GitHub release ---
gh release create v1.1.0 --title "v1.1.0 - Dark Mode" --notes "Adds dark mode toggle and theme persistence."

# --- Configure to always push annotated tags ---
git config --global push.followTags true
            

Shows 创建 annotated 标签使用消息, listing and 筛选标签, pushing 标签到 remote, tagging past 提交, 检查 out 发布, 创建 hotfix 分支从 标签, 删除标签, 创建 GitHub 发布, and 配置 automatic 标签 pushing. Annotated 标签使用 SemVer 是 professional 标准.

动手试一试

常见错误

错误写法

创建 lightweight 标签相反的 annotated 标签 for 发布

正确做法

Lightweight 标签存储否 metadata (否消息, author, or date). For 发布, 总是 use annotated 标签: git 标签 - v1.0.0 -m '发布消息'. Annotated 标签能 是 signed 使用 GPG, 存储发布信息, and 是 recommended 练习 for version marking.

错误写法

Forgetting 到推送标签 — 它们是 不 pushed 使用 git 推送

正确做法

git 推送仅 pushes 提交 and 分支, 不标签. 之后创建标签, 运行 git 推送原点 <标签> or git 推送原点 --标签到 推送它 到 remote. 设置 git 配置 --全局推送.followTags 真到 推送 annotated 标签 automatically 使用提交.

相关速查表

练习 git 标签

4 个练习

练习 git 标签

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误