目录
一、创建标签
二、查看标签
三、推送标签
四、删除标签
一、创建标签
标签tag,是对某次 commit 的⼀个标识,相当于起了⼀个别名。
相较于难以记住的 commit id , tag 很好的解决这个问题,因为 tag ⼀定要给⼀个让⼈容易记住,且有意义的名字。当我们需要回退到某个重要版本时,直接使⽤标签就能很快定位到。
# 切换到需要打标签的分支上,用 git tag [name] 给分支打标签
# 用 git tag 查看标签
(base) [root@localhost git-learning]# git branch
* master
(base) [root@localhost git-learning]# git tag v1.0
(base) [root@localhost git-learning]# git tag
v1.0
(base) [root@localhost git-learning]#
# 默认标签是打在最新提交的 commit 上,我们也可以指定 commit id 打标签
(base) [root@localhost git-learning]# git log --pretty=oneline --abbrev-commit
8d78346 add files: file.ini file.so
1ccab43 Git在线修改 file1.
ec3bb79 add first file
dd56a90 Initial commit
(base) [root@localhost git-learning]# git tag v0.0 1ccab43
(base) [root@localhost git-learning]# git tag
v0.0
v1.0
(base) [root@localhost git-learning]#
二、查看标签
git tag 查看标签不是按时间顺序列出,而是按字母排列的。
# git show [tagname] 查看标签具体信息
(base) [root@localhost git-learning]# git show v0.0
commit 1ccab432720ac02588358ed4c7054d1f96a03291
Author: 命运on9 <1210451061@qq.com>
Date: Wed Feb 7 08:02:08 2024 +0000
Git在线修改 file1.
Signed-off-by: 命运on9 <1210451061@qq.com>
diff --git a/file1 b/file1
index 8d0e412..00eb667 100644
--- a/file1
+++ b/file1
@@ -1 +1,2 @@
hello git
+Git 在线修改!
\ No newline at end of file
(base) [root@localhost git-learning]#
# Git还提供可以创建带有说明的标签,⽤-a指定标签名,-m指定说明⽂字
# 格式为:git tag -a [name] -m "XXX" [commit_id]
(base) [root@localhost git-learning]# tree .git
.git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── FETCH_HEAD
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ ├── heads
│ │ └── master
│ └── remotes
│ └── origin
│ ├── HEAD
│ └── master
├── objects
│ ├── 00
│ │ └── eb6679fe92307b0f6f582f036da3694817c35f
│ ├── 1c
│ │ └── cab432720ac02588358ed4c7054d1f96a03291
│ ├── 78
│ │ └── 4d778d166192ed8c2bf9443ae3765a83e17a33
│ ├── 8d
│ │ ├── 0e41234f24b6da002d962a26c2495ea16a425f
│ │ └── 78346e75380738d1aa190988ce310f109522e7
│ ├── 96
│ │ └── c7dd0e5376c33df611fce289241c1da2c5c826
│ ├── c3
│ │ └── 14d4d60463feca795fcc83b2b314a12d408787
│ ├── e7
│ │ └── 2ce89ba7ed78350231db5b72d2807a3cfbd56a
│ ├── ec
│ │ └── 3bb79ba9580fb15ecaeb21efb7391c11dd36d2
│ ├── info
│ └── pack
│ ├── pack-3ab36bb2fcbccbe256351f785976fe27f9fb5bf8.idx
│ └── pack-3ab36bb2fcbccbe256351f785976fe27f9fb5bf8.pack
├── ORIG_HEAD
├── packed-refs
└── refs
├── heads
│ └── master
├── remotes
│ └── origin
│ ├── HEAD
│ └── master
└── tags
├── v0.0
└── v1.0
24 directories, 38 files
(base) [root@localhost git-learning]#
三、推送标签
# 将标签推送到远程仓库
(base) [root@localhost git-learning]# git tag
v0.0
v1.0
(base) [root@localhost git-learning]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 724b2093
To git@gitee.com:hdu-a-chao/git-learning.git
* [new tag] v1.0 -> v1.0
(base) [root@localhost git-learning]#
四、删除标签
# 如果标签打错了,可以用 git tag -d [tagname] 删除标签
(base) [root@localhost git-learning]# git tag -d v0.0
已删除 tag 'v0.0'(曾为 1ccab43)
(base) [root@localhost git-learning]# git tag
v1.0
(base) [root@localhost git-learning]#
# 如果已经将标签推送到远程仓库,想要删除标签怎么办?
# 先在本地仓库删除,再将本地仓库推送到远程仓库
(base) [root@localhost git-learning]# git tag
v1.0
(base) [root@localhost git-learning]# git tag -d v1.0
已删除 tag 'v1.0'(曾为 8d78346)
(base) [root@localhost git-learning]# git push origin :refs/tags/v1.0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 274a87aa
To git@gitee.com:hdu-a-chao/git-learning.git
- [deleted] v1.0
(base) [root@localhost git-learning]#