How to use git tag

Original address: http://blog.sina.com.cn/s/blog_157abb48f0102wjcw.html

Label management

When publishing a version, we usually first put a label in the version library, so that the only version at the time of labeling is determined. Whenever a version of a certain label is taken in the future, it is to take out the historical version of that labeled time. Therefore, the tag is also a snapshot of the repository.

Although the Git tag is a snapshot of the repository, it is actually a pointer to a commit (much like a branch, right? But the branch can be moved, and the tag cannot be moved), so creating and deleting tags are done instantly. (That is, tag)

Create label

Tagging in Git is very simple. First, switch to the branch that needs to be tagged:

$ git branch
* dev
  master
$ git checkout master
Switched to branch 'master'

Then, git tagyou can type a new label by typing:

$ git tag v1.0

You can use the command to git tagview all tags:

$ git tag
v1.0

The default label is on the latest submitted commit. Sometimes, if I forget to label, for example, it is already Friday, but the label that should be labeled on Monday is not labeled, what should I do?

The method is to find the commit id of the historical submission, and then type it on:

$ git log --pretty=oneline --abbrev-commit
6a5819e merged bug fix 101
cc17032 fix bug 101
7825a50 merge with no-ff
6224937 add merge
59bc1cb conflict fixed
400b400 & simple
75a857c AND simple
fec145a branch test
d17efd8 remove test.txt
...

For example, if you want to add mergetag this submission, its corresponding commit id is 6224937, type the command:

$ git tag v0.9 6224937

Use the command to git tagview the label:

$ git tag
v0.9
v1.0

Note that the tags are not listed in chronological order, but sorted alphabetically. You can use to git showview tag information:

$ git show v0.9
commit 622493706ab447b6bb37e4e2a2f276a20fed2ab4
Author: Michael Liao 
Date:   Thu Aug 22 11:22:08 2013 +0800

    add merge
...

As you can see, v0.9it is indeed on add mergethis submission.

You can also create a label with a description, -aspecify the label name, and -mspecify the description text:

$ git tag -a v0.1 -m "version 0.1 released" 3628164

git showYou can see the description text with the command :

$ git show v0.1
tag v0.1
Tagger: Michael Liao <<span class="title">[email protected]>
Date:   Mon Aug 26 07:28:11 2013 +0800

version 0.1 released

commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <<span class="title">[email protected]>
Date:   Tue Aug 20 15:11:49 2013 +0800

    append GPL

还可以通过-s用私钥签名一个标签:

$ git tag -s v0.2 -m "signed version 0.2 released" fec145a

签名采用PGP签名,因此,必须首先安装gpg(GnuPG),如果没有找到gpg,或者没有gpg密钥对,就会报错:

gpg: signing failed: secret key not available
error: gpg failed to sign the data
error: unable to sign the tag

如果报错,请参考GnuPG帮助文档配置Key。

用命令git show 可以看到PGP签名信息:

$ git show v0.2
tag v0.2
Tagger: Michael Liao 
Date:   Mon Aug 26 07:28:33 2013 +0800

signed version 0.2 released
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (Darwin)

iQEcBAABAgAGBQJSGpMhAAoJEPUxHyDAhBpT4QQIAKeHfR3bo...
-----END PGP SIGNATURE-----

commit fec145accd63cdc9ed95a2f557ea0658a2a6537f
Author: Michael Liao 
Date:   Thu Aug 22 10:37:30 2013 +0800

    branch test

用PGP签名的标签是不可伪造的,因为可以验证PGP签名。验证签名的方法比较复杂,这里就不介绍了。

小结

  • 命令git tag 用于新建一个标签,默认为HEAD,也可以指定一个commitid;

  • git tag -a -m "blablabla..."可以指定标签信息;

  • git tag -s -m "blablabla..."可以用PGP签名标签;

  • 命令git tag可以查看所有标签。


操作标签:

如果标签打错了,也可以删除:

$ git tag -d v0.1
Deleted tag 'v0.1' (was e078af9)

因为创建的标签都只存储在本地,不会自动推送到远程。所以,打错的标签可以在本地安全删除。

如果要推送某个标签到远程,使用命令git push origin

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

或者,一次性推送全部尚未推送到远程的本地标签:

$ git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 554 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:michaelliao/learngit.git
 * [new tag]         v0.2 -> v0.2
 * [new tag]         v0.9 -> v0.9

如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:

$ git tag -d v0.9
Deleted tag 'v0.9' (was 6224937)

然后,从远程删除。删除命令也是push,但是格式如下:

$ git push origin :refs/tags/v0.9
To git@github.com:michaelliao/learngit.git
 - [deleted]         v0.9

要看看是否真的从远程库删除了标签,可以登陆GitHub查看。

小结

  • 命令git push origin 可以推送一个本地标签;

  • 命令git push origin --tags可以推送全部未推送过的本地标签;

  • 命令git tag -d 可以删除一个本地标签;

  • 命令git push origin :refs/tags/可以删除一个远程标签。(要先删除本地)


发布了60 篇原创文章 · 获赞 44 · 访问量 34万+

Guess you like

Origin blog.csdn.net/beyond702/article/details/78304326