【Git】-- 标签管理
文章目录
更多Git相关知识: Git专栏
1. 标签管理
1.1 标签的作用
标签tag,可以简单的理解为是对某次commit 的一个标识,相当于起了一个别名。例如,在项目发布某个版本的时候,针对最后一次commit 起一个v1.0 这样的标签来标识里程碑的意义。
这有什么用呢?相较于难以记住的 commit id,tag 很好的解决这个问题,因为 tag 一定要给一个让人容易记住,且有意义的名字。当我们需要回退到某个重要版本时,直接使用标签就能很快定位到。
1.2 操作标签
打标签是针对提交打的标签。
1.3 创建标签
该操作会默认给我们最新的依次提交打上v1.0的标签。
root@VM-0-3-ubuntu:~/remote-gitcode# git tag v1.01.4 查看有哪些标签
root@VM-0-3-ubuntu:~/remote-gitcode# git tag v1.0 或者
root@VM-0-3-ubuntu:~/remote-gitcode# tree .git .git ├── branches ├── COMMIT_EDITMSG ├── config ├── description ├── FETCH_HEAD ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── pre-merge-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ ├── pre-receive.sample │ ├── push-to-checkout.sample │ ├── sendemail-validate.sample │ └── update.sample ├── index ├── info │ └── exclude ├── logs │ ├── HEAD │ └── refs │ ├── heads │ │ └── master │ └── remotes │ └── origin │ ├── HEAD │ ├── master │ └── mater ├── objects │ ├── 05 │ │ └── fe86c3cd7ec2287a059a43b68cdc45f79cc43b │ ├── 09 │ │ └── 9c367d45aa723611f3721038110dc2c7a9eecf │ ├── 13 │ │ └── b46fa6ad76bb659f965fd48c86ed49842aecc3 │ ├── 30 │ │ └── 0659b8f65f8f6ca9160b7154a62c06359772f8 │ ├── 3f │ │ └── 14f45e2fbcd94d370f0a2b0d1ef194ac8c69c5 │ ├── 45 │ │ └── 23b2b9da80d0b335e1652f6070b4de1568b906 │ ├── 4b │ │ └── c8161e604f7e008eacccfdcae36171e119d1e9 │ ├── 8d │ │ └── 0e41234f24b6da002d962a26c2495ea16a425f │ ├── a5 │ │ └── 421d46e76e71c849d56ef0dfd2c64433a2e553 │ ├── f4 │ │ └── 179bfa0f591af2b60a933413a0fd0499dc3ba1 │ ├── info │ └── pack │ ├── pack-c20b9d41255e962915ba1d4372cfe6478f7ec66e.idx │ ├── pack-c20b9d41255e962915ba1d4372cfe6478f7ec66e.pack │ └── pack-c20b9d41255e962915ba1d4372cfe6478f7ec66e.rev ├── ORIG_HEAD ├── packed-refs └── refs ├── heads │ └── master ├── remotes │ └── origin │ ├── HEAD │ ├── master │ └── mater └── tags └── v1.0 
1.5 查看标签中存放的是什么
root@VM-0-3-ubuntu:~/remote-gitcode# cat .git/refs/tags/v1.0 099c367d45aa723611f3721038110dc2c7a9eecf 查看提交日志:
root@VM-0-3-ubuntu:~/remote-gitcode# git log commit 099c367d45aa723611f3721038110dc2c7a9eecf (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) Author: pepper-cloth <[email protected]> Date: Mon Jan 1213:34:23 2026 +0800 add .git ignore commit 300659b8f65f8f6ca9160b7154a62c06359772f8 Author: cuckoo <[email protected]> Date: Mon Jan 12 05:14:43 2026 +0000 update file.txt. Signed-off-by: cuckoo <[email protected]>....... 可以看到,v1.0标签里卖弄存放的就是最后一次提交的commit id。
1.6 给前面的提交打标签
给前面的提交打标签,需要拿到前面提交的commit id。
获取前面提交的commit id:
root@VM-0-3-ubuntu:~/remote-gitcode# git log --pretty=oneline --abbrev-commit 099c367 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD)add .git ignore 300659b update file.txt. f4179bf (origin/mater)add file.txt f59a47a Initial commit 打标签:
root@VM-0-3-ubuntu:~/remote-gitcode# git tag v0.9 300659b root@VM-0-3-ubuntu:~/remote-gitcode# git tag v0.9 v1.0 1.7 给标签添加描述
root@VM-0-3-ubuntu:~/remote-gitcode# git tag -a v0.8 -m "important tag: xxx" f4179bf root@VM-0-3-ubuntu:~/remote-gitcode# git tag v0.8 v0.9 v1.0 root@VM-0-3-ubuntu:~/remote-gitcode# git show v0.8 tag v0.8 Tagger: pepper-cloth <[email protected]> Date: Mon Jan 1220:35:09 2026 +0800 important tag: xxx commit f4179bfa0f591af2b60a933413a0fd0499dc3ba1 (tag: v0.8, origin/mater) Author: pepper-cloth <[email protected]> Date: Mon Jan 1213:05:24 2026 +0800 add file.txt diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..8d0e412 --- /dev/null +++ b/file.txt @@ -0,0 +1 @@ +hello git1.8 删除标签
root@VM-0-3-ubuntu:~/remote-gitcode# git tag -d v0.9 Deleted tag 'v0.9'(was 300659b) root@VM-0-3-ubuntu:~/remote-gitcode# git tag v0.8 v1.0 1.3 推送标签
1.3.1 单个标签推送
root@VM-0-3-ubuntu:~/remote-gitcode# git push origin v1.0 Total 0(delta 0), reused 0(delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag f3477ba6 To gitee.com:pepper-cloth/remote-gitcode.git * [new tag] v1.0 -> v1.0 
1.3.2 多个标签推送
root@VM-0-3-ubuntu:~/remote-gitcode# git push origin --tags Enumerating objects: 1, done. Counting objects: 100% (1/1), done. Writing objects: 100% (1/1), 166 bytes |166.00 KiB/s, done. Total 1(delta 0), reused 0(delta 0), pack-reused 0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag 9440c2a3 To gitee.com:pepper-cloth/remote-gitcode.git * [new tag] v0.8 -> v0.8 
1.3.3 删除标签并推送
root@VM-0-3-ubuntu:~/remote-gitcode# git push origin :v1.0 remote: Powered by GITEE.COM [1.1.23] remote: Set trace flag ac4ae0a3 To gitee.com:pepper-cloth/remote-gitcode.git - [deleted] v1.0 root@VM-0-3-ubuntu:~/remote-gitcode# git tag v0.8