Gittag and versionmanagement

Gittag is 用于标记specificsubmitting 引用, 通常用于标记releaseversion. versionmanagement is usingGitfor软件version控制 important 部分, throughtag可以方便地management and rollback to specificversion.

1. tag concepts and 作用

tag is 指向specificsubmitting 引用, and branch不同, tag不会随着 new submitting而move. tag通常用于标记 important versionrelease点, such asv1.0.0, v2.0.0etc..

1.1 tag 作用

2. tag class型

Gitsupport两种class型 tag: 轻量tag (Lightweight Tag) and 附注tag (Annotated Tag) .

2.1 轻量tag (Lightweight Tag)

轻量tag is 指向specificsubmitting simple 指针, class似于branch, 但不会move. 它只package含submitting 哈希值, 不package含otherinformation.

2.2 附注tag (Annotated Tag)

附注tag is store in Gitdatalibraryin 完整object, package含标signature称, tagcreation者, creation日期, taginformationetc.详细information. 它 is 推荐using tagclass型, 尤其 is in 公Development布 versionin.

3. creationtag

Gitproviding了creation轻量tag and 附注tag commands.

3.1 creation轻量tag

using git tag commands可以creation轻量tag.

# creation轻量tag git tag v1.0.0 # 基于specificsubmittingcreation轻量tag git tag v1.0.0 submitting-hash

3.2 creation附注tag

using git tag -a commands可以creation附注tag, 并using -m parameter添加taginformation.

# creation附注tag并添加taginformation git tag -a v1.0.0 -m "Version 1.0.0 release" # 基于specificsubmittingcreation附注tag git tag -a v1.0.0 submitting-hash -m "Version 1.0.0 release" # creation带signature 附注tag (需要GPGkey) git tag -s v1.0.0 -m "Version 1.0.0 release"

提示

建议using附注tag, 因 for 它package含更 many 元datainformation, 便于versionmanagement and 追溯.

4. 查看tag

using git tag commands可以查看仓libraryin 所 has tag.

# 查看所 has tag git tag # 查看符合specific模式 tag git tag -l "v1.*" # 查看tag 详细information git show v1.0.0 # 只查看taginformation, 不显示submitting in 容 git show --name-only v1.0.0

5. pushtag to 远程仓library

默认circumstances under , git push commands不会pushtag to 远程仓library, 需要using git push origin tagname commands显式pushtag.

# push单个tag to 远程仓library git push origin v1.0.0 # push所 has tag to 远程仓library git push origin --tags # push所 has 附注tag to 远程仓library git push origin --follow-tags

6. checkouttag

using git checkout commands可以checkoutspecifictag code, 进入分离头指针status.

# checkouttag git checkout v1.0.0 # 基于tagcreation new branch git checkout -b branch-name v1.0.0

Notes

in 分离头指针status under for submitting不会属于任何branch, easy loss. such as果需要 in tagBasics on formodify, 建议先creation一个 new branch.

7. deletetag

using git tag -d commands可以delete本地tag, using git push origin --delete tagname commands可以delete远程仓libraryin tag.

# delete本地tag git tag -d v1.0.0 # delete远程仓libraryin tag git push origin --delete v1.0.0 # 另一种delete远程tag 方式 git push origin :refs/tags/v1.0.0

8. tag best practices

以 under is usingGittag 一些best practices:

9. versionmanagement策略

良 good versionmanagement策略可以improving软件 quality and 可maintenance性. 以 under is 几种common versionmanagement策略:

9.1 语义化version控制 (Semantic Versioning)

语义化version控制 is aversion命名规范, 格式 for MAJOR.MINOR.PATCH:

9.2 releasebranch策略

结合branch and tagforversionmanagement is acommon 策略:

10. versionrollback

usingGittag可以方便地rollback to specificversion code.

# 查看taglist git tag # checkoutspecifictag code git checkout v1.0.0 # such as果需要 in 该versionBasics on formodify, creation一个 new branch git checkout -b rollback-v1.0.0 # or 者直接将masterbranchreset to specifictag git checkout master git reset --hard v1.0.0

warning

using git reset --hard commands会永久delete未submitting 更改, using时请务必谨慎!such as果需要rollback远程仓libraryin code, 需要using git push -f commands强制push, 这可能会影响otherteam members, 建议 in 团队 in 部协商 after for.

实践case: usingtagmanagementversionrelease

  1. 确保当 before in master branch: git checkout master
  2. pull最 new code: git pull
  3. creation一个附注tag, 标记1.0.0versionrelease: git tag -a v1.0.0 -m "Version 1.0.0 release - First stable release"
  4. 查看tag is 否creation成功: git tag
  5. 查看tag 详细information: git show v1.0.0
  6. pushtag to 远程仓library: git push origin v1.0.0
  7. in 远程仓library (such asGitHub) on 查看tag is 否push成功
  8. checkouttagcode: git checkout v1.0.0
  9. 基于tagcreation一个 new branch: git checkout -b release-v1.0.0
  10. 切换回 master branch: git checkout master

互动练习

请completion以 under 练习, 巩固Gittag and versionmanagement:

  1. for 当 before branchcreation一个轻量tag v1.0.0
  2. 查看已creation tag
  3. delete刚刚creation 轻量tag
  4. creation一个附注tag v1.0.0, 并添加详细 taginformation
  5. 查看附注tag 详细information
  6. pushtag to 远程仓library
  7. in 远程仓library on verificationtag is 否push成功
  8. checkouttagcode, 进入分离头指针status
  9. 基于tagcreation一个 new branch
  10. delete本地tag and 远程tag

completion练习 after , 你应该able to熟练MasterGittag and versionmanagement basicoperation!