10 Git —— 标签管理
本节内容:
命令git tag用于新建一个标签,默认为HEAD,也可以指定一个commit id;命令git tag -a -m "blablabla..."可以指定标签信息;命令git tag可以查看所有标签。命令git push origin 可以推送一个本地标签;命令git push origin --tags可以推送全部未推送过的本地标签;命令git tag -d 可以删除一个本地标签;命令git push origin :refs/tags/ 可以删除一个远程标签。
一 、创建标签
在Git中打标签非常简单,首先切换到需要打标签的分支上:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git branch dev* masterlwenhaodeMacBook-Pro:TestGit lwenhao$
命令git tag <name>
就可以打一个新标签:
git tag v1.0
可以用命令git tag
查看所有标签:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git tagv1.0lwenhaodeMacBook-Pro:TestGit lwenhao$
默认标签是打在最新提交的commit上的。有时候,如果忘了打标签,比如,现在已经是周五了,但应该在周一打的标签没有打,怎么办?
方法是找到历史提交的commit id,然后打上就可以了:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git log --pretty=oneline --abbrev-commit99cd95c (HEAD -> master, tag: v1.0, origin/master, origin/HEAD) 再来点93c5700 再写点内容0ba4267 merged bug fix openfirec0e8f25 dev to masterafd1966 merge with no-ff8faa495 add 学习615c264 conflict fixed426ee67 add 我要提交8b01682 update readme.mdfca856d branch test38e1bd3 Initial commitlwenhaodeMacBook-Pro:TestGit lwenhao$
比方说要对merged bug fix openfire
这次提交打标签,它对应的commit id是0ba4267
,敲入命令:
git tag v_0.8 0ba4267
再用命令git tag
查看标签:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git tagv1.0v_0.8lwenhaodeMacBook-Pro:TestGit lwenhao$
注意,标签不是按时间顺序列出,而是按字母排序的。可以用git show <tagname>
查看标签信息:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git show v_0.8commit 0ba42671a829071913a6b74d5c642fdf7e1c2531 (tag: v_0.8)Merge: afd1966 c0e8f25Author: lwenhaoDate: Thu Feb 21 18:59:22 2019 +0800 merged bug fix openfirelwenhaodeMacBook-Pro:TestGit lwenhao$
可以看到,v_0.8
确实打在 merged bug fix openfire
这次提交上。
还可以创建带有说明的标签,用-a
指定标签名,-m
指定说明文字:
git tag -a v0.1 -m "version 0.1" 38e1bd3
用命令git show <tagname>
可以看到说明文字:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git show v0.1tag v0.1Tagger: lwenhaoDate: Fri Feb 22 00:32:01 2019 +0800version 0.1commit 38e1bd3e0600fac25d7e48b1493b5188e1a348f6 (tag: v0.1)Author: lwenhao <1433233919@qq.com>Date: Wed Feb 20 23:27:55 2019 +0800 Initial commitdiff --git a/README.md b/README.mdnew file mode 100644index 0000000..191a9ad--- /dev/null+++ b/README.md@@ -0,0 +1 @@+# TestGit\ No newline at end of filelwenhaodeMacBook-Pro:TestGit lwenhao$
注意:标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。
二、操作标签
如果标签打错了,也可以删除:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git tag -d v_0.8Deleted tag 'v_0.8' (was 0ba4267)lwenhaodeMacBook-Pro:TestGit lwenhao$
因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。
如果要推送某个标签到远程,使用命令git push origin <tagname>
:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin v1.0Total 0 (delta 0), reused 0 (delta 0)remote: This repository moved. Please use the new location:remote: https://github.com/lwenhaoCN/TestGit.gitTo https://github.com/liuwenhaoCN/TestGit.git * [new tag] v1.0 -> v1.0lwenhaodeMacBook-Pro:TestGit lwenhao$
或者,一次性推送全部尚未推送到远程的本地标签:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin --tagsCounting objects: 1, done.Writing objects: 100% (1/1), 152 bytes | 152.00 KiB/s, done.Total 1 (delta 0), reused 0 (delta 0)remote: This repository moved. Please use the new location:remote: https://github.com/lwenhaoCN/TestGit.gitTo https://github.com/liuwenhaoCN/TestGit.git * [new tag] v0.1 -> v0.1lwenhaodeMacBook-Pro:TestGit lwenhao$
如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git tag -d v0.1Deleted tag 'v0.1' (was 4496714)lwenhaodeMacBook-Pro:TestGit lwenhao$
然后,从远程删除。删除命令也是push,但是格式如下:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git push origin :refs/tags/v0.1remote: This repository moved. Please use the new location:remote: https://github.com/lwenhaoCN/TestGit.gitTo https://github.com/liuwenhaoCN/TestGit.git - [deleted] v0.1lwenhaodeMacBook-Pro:TestGit lwenhao$
要看看是否真的从远程库删除了标签,可以登陆GitHub查看。