Wlgls 冲鸭!

Git标签

2019-07-02
Git

在使用git的某些功能的时候,我们总需要使用commit-id,但是一长串数字实在难以记录,所以我们可以给版本打上标签

创建标签

  • git tag <tagname>

    用来新建一个标签,默认是最新的一次提交

    也可以指定一个commit id,使用git tag <tagname> <commit id>

    如果加上-m参数则可以指定标签信息


在git中打标签,首先需要切换到打标签的分支上,例如

$: git branch
* dev
  master
$: git checkout master

现在就可以打上一个新标签了,默认的标签是打在最新提交的commit上的

$: git tag v1.0

如果我们希望给以前的commit打上标签,则可以使用git log找到commit id,然打上就好了

$: git log --pretty=oneline --abbrev-commit
5dc0fe6 (HEAD -> master, tag: v1.0, origin/master) merge ten
112a8f5 ten
2660d45 bug fixed merge dev
2a6dd4c merge test
01fa52f merged fix bug
6438fab fix bug
a0758fa merge with --no-f two
14ade7e ninth
2e2604d merge with --no-f
5f2e123 eighth
02c590f conflict fixed
2e910ea sixth
4d5aa1d fifth
021ef09 forth
f83dd14 third
06283ca second
805e981 first
$: git tag v0.10 -m "version 0.10 released" 112a8f5

然后还可以使用命令git tag来查看所有标签

$: git tag
v1.0

我们也可以使用git show <tagname>来查看说明文字

$: git show v0.10
tag v0.10
Tagger: smithgua <206007768@qq.com>
Date:   Tue Jul 2 23:05:27 2019 +0800

version 0.10 released

commit 112a8f5e38a3330efe44d1f6ef512d471cf19375 (tag: v0.10)
Author: smithgua <206007768@qq.com>
Date:   Tue Jul 2 21:14:48 2019 +0800

    ten

diff --git a/learngit.txt b/learngit.txt
new file mode 100644
index 0000000..e48b2f4
--- /dev/null
+++ b/learngit.txt
@@ -0,0 +1 @@
+ten

标签操作

删除标签

  • git tag -d <tagname>

    删除本地标签


$: git tag -d v0.10
已删除标签 'v0.10'(曾为 7c1fe91)

推送标签到远程

  • git push origin <tagname>

    将本地标签推送到远程库,如果将<tagname>改为--tags则可以推送所有没有推送过的标签


推送指定标签

$: git push origin v0.10
对象计数中: 1, 完成.
写入对象中: 100% (1/1), 163 bytes | 163.00 KiB/s, 完成.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/Wlgls/learngit.git
 * [new tag]         v0.10 -> v0.10

推送全部标签

$: git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Wlgls/learngit.git
 * [new tag]         v1.0 -> v1.0

删除远程标签

  • git push origin :refs/tags/<tagname>

    可以删除一个远程标签


首先删除本地,然后再在远程删除,有特定格式

$: git tag -d vo.10
已删除标签 'v0.10'(曾为 a7354e0)
$: git push origin :refs/tag/v0.10
remote: warning: Deleting a non-existent ref.
To https://github.com/Wlgls/learngit.git
 - [deleted]         refs/tag/v0.10