Skip to content

标签 and 发布项目

简单 约 15 分钟

你将构建什么

仓库使用多个 version 标签 (v1.0.0, v1.1.0, v2.0.0) representing 发布. 你将 inspect, checkout, and 推送标签到 share 发布点.

学习目标

  • 创建 annotated 标签使用 git 标签 -
  • 列表 and inspect 标签使用 git 标签 and git show
  • Checkout tagged version 到 inspect past 发布
  • 推送标签到 remote 仓库

前置要求

  • git init, 添加, 提交

步骤

  1. 1

    设置项目使用多个提交

    创建仓库 and 制作 three 提交 representing three versions 的简单 app.

    代码
    mkdir tag-project
    cd tag-project
    git init
    echo "v1.0" > version.txt
    git add .
    git commit -m "Initial release v1.0"
    echo "v1.1" > version.txt
    git commit -am "Update to v1.1"
    echo "v2.0" > version.txt
    git commit -am "Major update to v2.0"

    预期结果

    仓库使用 three 提交, 每个更新 version.txt.

  2. 2

    创建 annotated 标签 for 每个发布

    标签首先提交 as v1.0.0, 其次 as v1.1.0, and current as v2.0.0. Annotated 标签存储消息 and metadata.

    代码
    git tag -a v1.0.0 HEAD~2 -m "First stable release"
    git tag -a v1.1.0 HEAD~1 -m "Minor feature update"
    git tag -a v2.0.0 -m "Major release with breaking changes"

    预期结果

    Three annotated 标签 marking 发布点 in history.

  3. 3

    列表 and inspect 标签

    列表所有标签 and inspect 特定标签到 see 它的 metadata and 提交它 点到.

    代码
    git tag
    git show v1.0.0

    预期结果

    git 标签列表 v1.0.0, v1.1.0, v2.0.0; git show displays 标签 details.

  4. 4

    Checkout past 发布

    Checkout v1.0.0 标签到 see 项目 as 它是 at 那个发布. 你输入 detached HEAD 状态 — 这个是 expected for inspecting past version.

    代码
    git checkout v1.0.0
    cat version.txt  # shows v1.0
    git checkout main  # return to latest

    预期结果

    version.txt shows v1.0 at 标签, 然后返回到 v2.0 on 主要.

  5. 5

    推送标签到 remote

    标签是 局部仅 通过默认. 推送它们到 share 发布点 使用 team. 你能 推送所有标签 or 特定 ones.

    代码
    # Push a specific tag
    git push origin v2.0.0
    # Or push all tags at once
    git push --tags

    预期结果

    标签是 现在 available on remote 仓库.

自己动手试试

你学到了什么

你创建 annotated 标签 for version 发布, inspected 它们, 检查 out past 发布, and pushed 标签到 remote. 标签是 标准方式到 mark 发布点 in Git history.

相关资源

相关课程

  • git 标签

    学习 annotated and lightweight 标签.

相关速查表

查阅不认识的术语

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

查看术语表

解读错误信息

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

查看错误

← 返回主题