runsisi's

technical notes

git tag

2019-04-14 runsisi#git

git 可以创建两种类型的 tag,一种类似于 commit 的别名,没有任何多余的信息附加在该 tag 之上,称作 lightweight tag,如果在创建 tag 时不指定 -a, -s-m 中的任一选项,那么创建出来的 tag 就是 lightweight tag;还一种类似于 commit 本身,有独立的 SHA 标示的 tag 对象,用于记录 tag 的基础信息,称作 annotated tag。

以如下 commit 为例,分别进行解释:

~$ git log -1
commit 857f66fc24aca85da1af5e8253cbde4c30fe57dd (HEAD -> master, origin/master, origin/HEAD)
Author: luo.runbing <luo.runbing@example.com>
Date:   Fri Apr 12 20:03:16 2019 +0800

    add example override rules$ git show-ref --tags
857f66fc24aca85da1af5e8253cbde4c30fe57dd refs/tags/v1.0-light
    
    Signed-off-by: luo.runbing <luo.runbing@example.com>

lightweight tag

创建一个 lightweight 类型的 tag:

~$ git tag -l
~$ git tag v1.0-light
~$ git tag -l
v1.0-light

验证该 tag 的类型为 commit:

~$ git cat-file -t v1.0-light
commit

该 tag 直接指向 commit 857f66f:

~$ git show-ref --tags
857f66fc24aca85da1af5e8253cbde4c30fe57dd refs/tags/v1.0-light
~$ cat .git/refs/tags/v1.0-light 
857f66fc24aca85da1af5e8253cbde4c30fe57dd

同时,tag v1.0-light 之前最新的 commit 记录显示为 857f66f:

~$ git rev-list v1.0-light
857f66fc24aca85da1af5e8253cbde4c30fe57dd
7dd8f1f6771bc40860a5e89c559bcdbe05c0f358
...

查看该 tag 对象,直接显示的是 commit 857f66f 的信息:

~$ git show v1.0-light
commit 857f66fc24aca85da1af5e8253cbde4c30fe57dd (HEAD -> master, tag: v1.0-light, origin/master, origin/HEAD)
Author: luo.runbing <luo.runbing@example.com>
Date:   Fri Apr 12 20:03:16 2019 +0800

    add example override rules
    
    Signed-off-by: luo.runbing <luo.runbing@example.com>

diff --git a/cmd/iptables/examples.d/override.rules b/cmd/iptables/examples.d/override.rules
new file mode 100644
index 0000000..096d5fb
--- /dev/null
+++ b/cmd/iptables/examples.d/override.rules
...

查看底层信息,实际上查看的是指向的 commit 857f66f 的底层信息:

~$ git cat-file -p v1.0-light
tree 3b4f701b37e23bef4fe1a6be2b6a3e0232645098
parent 7dd8f1f6771bc40860a5e89c559bcdbe05c0f358
author luo.runbing <luo.runbing@example.com> 1555070596 +0800
committer luo.runbing <luo.runbing@example.com> 1555142660 +0800
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyxmAYACgkQ2jv9z413
 9nU67gv/Zm39ZpNRL+AUqRHr5RL7lVhjkNw/FjwSfWxRiZwGxCmQAWCi3pM9efS8
 +MJUIPPsBF9IkM49Fts8RnFacn+RuVYhjXoJhAmzKgEB9ty1Ihp8zgYul8YpU4v7
 WIIl9Vg7kFUFGs1YAdikgGYmGroq3DYRFEOkMHj+cDwjaJSCg+EKzPZEBQPPQyfv
 Nc8p/VK4N3/2myiTclqvRq9hTMLbrnLfK/y//XS4uVk6f55Tnn0SFttS7utG6llU
 SC7wQqFZQzjAKxoILjMmyuZSBTG8PACLeAnWgFt+w0jCMU/BSkQxkbM5M5F7d6Aj
 rOAkz2BC639hq22mUmkg+WQL1ycAIfcTo56m6Az4pxks2690ieFxnZMKAPpg/NWP
 NEwIfxyQCLPBXElvnKyhwTwEUN4r/LE1PDt/WtA+U7O7cAIQhPxQBaAYP4ubAtuC
 uUIB2rvhARo+Od/1xRK88v2iYcVlwXfsi8phbK8eRe5HvJEEcHDzhBUjGSwl9H85
 lkvWesbp
 =3mqv
 -----END PGP SIGNATURE-----

add example override rules

Signed-off-by: luo.runbing <luo.runbing@example.com>
~$ git cat-file -p 857f66f
tree 3b4f701b37e23bef4fe1a6be2b6a3e0232645098
parent 7dd8f1f6771bc40860a5e89c559bcdbe05c0f358
author luo.runbing <luo.runbing@example.com> 1555070596 +0800
committer luo.runbing <luo.runbing@example.com> 1555142660 +0800
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyxmAYACgkQ2jv9z413
 9nU67gv/Zm39ZpNRL+AUqRHr5RL7lVhjkNw/FjwSfWxRiZwGxCmQAWCi3pM9efS8
 +MJUIPPsBF9IkM49Fts8RnFacn+RuVYhjXoJhAmzKgEB9ty1Ihp8zgYul8YpU4v7
 WIIl9Vg7kFUFGs1YAdikgGYmGroq3DYRFEOkMHj+cDwjaJSCg+EKzPZEBQPPQyfv
 Nc8p/VK4N3/2myiTclqvRq9hTMLbrnLfK/y//XS4uVk6f55Tnn0SFttS7utG6llU
 SC7wQqFZQzjAKxoILjMmyuZSBTG8PACLeAnWgFt+w0jCMU/BSkQxkbM5M5F7d6Aj
 rOAkz2BC639hq22mUmkg+WQL1ycAIfcTo56m6Az4pxks2690ieFxnZMKAPpg/NWP
 NEwIfxyQCLPBXElvnKyhwTwEUN4r/LE1PDt/WtA+U7O7cAIQhPxQBaAYP4ubAtuC
 uUIB2rvhARo+Od/1xRK88v2iYcVlwXfsi8phbK8eRe5HvJEEcHDzhBUjGSwl9H85
 lkvWesbp
 =3mqv
 -----END PGP SIGNATURE-----

add example override rules

Signed-off-by: luo.runbing <luo.runbing@example.com>

如果不加上 --tags 选项,那么 git describe 将忽略 lightweight tag:

~$ git describe
fatal: No annotated tags can describe '857f66fc24aca85da1af5e8253cbde4c30fe57dd'.
However, there were unannotated tags: try --tags.
$ git describe --tags
v1.0-light

lightweight tag 只能校验 commit 的 gpg 签名:

~$ git verify-tag v1.0-light
error: v1.0-light: cannot verify a non-tag object of type commit.
~$ git verify-commit v1.0-light
gpg: Signature made Sat 13 Apr 2019 04:04:22 PM CST
gpg:                using RSA key DFD781A7C8334975F2FD0DC7DA3BFDCF8D77F675
gpg: Good signature from "luo.runbing (https://www.example.com/) <luo.runbing@example.com>" [ultimate]
gpg:                 aka "runsisi (https://runsisi.com/) <runsisi@hust.edu.cn>" [ultimate]

annotated tag

创建一个 annotated 类型的 tag:

~$ git tag -l
v1.0-light
~$ git tag -as -m 'annotated tag' v1.0
~$ git tag -l
v1.0
v1.0-light

验证该 tag 的类型为 tag:

~$ git cat-file -t v1.0
tag

该 tag 指向独立的 tag 对象:

~$ git show-ref --tags
f1fd82f39abf9abb767d68bee9febc20352af19b refs/tags/v1.0
857f66fc24aca85da1af5e8253cbde4c30fe57dd refs/tags/v1.0-light
~$ cat .git/refs/tags/v1.0
f1fd82f39abf9abb767d68bee9febc20352af19b

但与 lightweight tag 类似,tag v1.0 之前最新的 commit 记录也显示为 857f66f:

~$ git rev-list v1.0
857f66fc24aca85da1af5e8253cbde4c30fe57dd
7dd8f1f6771bc40860a5e89c559bcdbe05c0f358

查看该 tag 对象,显示的是 tag 对象 f1fd82f 的信息:

~$ git show v1.0
tag v1.0
Tagger: luo.runbing <luo.runbing@example.com>
Date:   Sun Apr 14 22:25:29 2019 +0800

annotated tag
-----BEGIN PGP SIGNATURE-----

iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyzQtkACgkQ2jv9z413
9nX7tQv8DPDjhZAu4gJ2oVlX8emY46T7mBxUG/3mBW1hKdJwDcdNFiw8EhPDspmF
ORjlVGZCtiGHPDoYXkZCTBbNN4+LhE3o7Envoq3wsqLUQ2125Y8jS1UqV6M1Ogpd
oYlYkYONXTHardn8aYZcKIjuZdVvIvUtqEme55Tv2yfDfIR/CJY834Zjpx1eMKND
DqCqgxE1d8Z0ps2o3z774+yxuSY5eWcdzXWn+nZAS/SDTS3TL7SiUPgPfMpWbp+h
hvt9igxrZeBQebJxOOgKtK7kEnJCPFr6EWZ4xXxK5qDUugkS6tF8GztLBO5IqMIz
ebaK7tI6DPpbNQ1cj4OAG36QdSG7l5fCjiB1eomvRUFxFT7BvzUfNLMOd3g6OuOi
bgH/zUH0vVwdCmETO7hnWdFF8vOV3KkE6BfjMVYfa9c4vY0Y4CadowOa9KxW/xxT
U8Ayk9eLPMkoq2b2fqwIxzTgavDl1mbZSnoglNRuYo/5YBHjnhPQXxoJvEX/qCkX
h5DKkpzW
=iXYy
-----END PGP SIGNATURE-----

commit 857f66fc24aca85da1af5e8253cbde4c30fe57dd (HEAD -> master, tag: v1.0-light, tag: v1.0, origin/master, origin/HEAD)
Author: luo.runbing <luo.runbing@example.com>
Date:   Fri Apr 12 20:03:16 2019 +0800

    add example override rules
    
    Signed-off-by: luo.runbing <luo.runbing@example.com>

diff --git a/cmd/iptables/examples.d/override.rules b/cmd/iptables/examples.d/override.rules
new file mode 100644
index 0000000..096d5fb
--- /dev/null
+++ b/cmd/iptables/examples.d/override.rules
...
~$ git show f1fd82f
tag v1.0
Tagger: luo.runbing <luo.runbing@example.com>
Date:   Sun Apr 14 22:25:29 2019 +0800

annotated tag
-----BEGIN PGP SIGNATURE-----

iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyzQtkACgkQ2jv9z413
9nX7tQv8DPDjhZAu4gJ2oVlX8emY46T7mBxUG/3mBW1hKdJwDcdNFiw8EhPDspmF
ORjlVGZCtiGHPDoYXkZCTBbNN4+LhE3o7Envoq3wsqLUQ2125Y8jS1UqV6M1Ogpd
oYlYkYONXTHardn8aYZcKIjuZdVvIvUtqEme55Tv2yfDfIR/CJY834Zjpx1eMKND
DqCqgxE1d8Z0ps2o3z774+yxuSY5eWcdzXWn+nZAS/SDTS3TL7SiUPgPfMpWbp+h
hvt9igxrZeBQebJxOOgKtK7kEnJCPFr6EWZ4xXxK5qDUugkS6tF8GztLBO5IqMIz
ebaK7tI6DPpbNQ1cj4OAG36QdSG7l5fCjiB1eomvRUFxFT7BvzUfNLMOd3g6OuOi
bgH/zUH0vVwdCmETO7hnWdFF8vOV3KkE6BfjMVYfa9c4vY0Y4CadowOa9KxW/xxT
U8Ayk9eLPMkoq2b2fqwIxzTgavDl1mbZSnoglNRuYo/5YBHjnhPQXxoJvEX/qCkX
h5DKkpzW
=iXYy
-----END PGP SIGNATURE-----

commit 857f66fc24aca85da1af5e8253cbde4c30fe57dd (HEAD -> master, tag: v1.0-light, tag: v1.0, origin/master, origin/HEAD)
Author: luo.runbing <luo.runbing@example.com>
Date:   Fri Apr 12 20:03:16 2019 +0800

    add example override rules
    
    Signed-off-by: luo.runbing <luo.runbing@example.com>

diff --git a/cmd/iptables/examples.d/override.rules b/cmd/iptables/examples.d/override.rules
new file mode 100644
index 0000000..096d5fb
--- /dev/null
+++ b/cmd/iptables/examples.d/override.rules
...

查看底层信息,查看的是 tag 对象 f1fd82f 的信息:

~$ git cat-file -p v1.0
object 857f66fc24aca85da1af5e8253cbde4c30fe57dd
type commit
tag v1.0
tagger luo.runbing <luo.runbing@example.com> 1555251929 +0800

annotated tag
-----BEGIN PGP SIGNATURE-----

iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyzQtkACgkQ2jv9z413
9nX7tQv8DPDjhZAu4gJ2oVlX8emY46T7mBxUG/3mBW1hKdJwDcdNFiw8EhPDspmF
ORjlVGZCtiGHPDoYXkZCTBbNN4+LhE3o7Envoq3wsqLUQ2125Y8jS1UqV6M1Ogpd
oYlYkYONXTHardn8aYZcKIjuZdVvIvUtqEme55Tv2yfDfIR/CJY834Zjpx1eMKND
DqCqgxE1d8Z0ps2o3z774+yxuSY5eWcdzXWn+nZAS/SDTS3TL7SiUPgPfMpWbp+h
hvt9igxrZeBQebJxOOgKtK7kEnJCPFr6EWZ4xXxK5qDUugkS6tF8GztLBO5IqMIz
ebaK7tI6DPpbNQ1cj4OAG36QdSG7l5fCjiB1eomvRUFxFT7BvzUfNLMOd3g6OuOi
bgH/zUH0vVwdCmETO7hnWdFF8vOV3KkE6BfjMVYfa9c4vY0Y4CadowOa9KxW/xxT
U8Ayk9eLPMkoq2b2fqwIxzTgavDl1mbZSnoglNRuYo/5YBHjnhPQXxoJvEX/qCkX
h5DKkpzW
=iXYy
-----END PGP SIGNATURE-----
~$ git cat-file -p f1fd82f
object 857f66fc24aca85da1af5e8253cbde4c30fe57dd
type commit
tag v1.0
tagger luo.runbing <luo.runbing@example.com> 1555251929 +0800

annotated tag
-----BEGIN PGP SIGNATURE-----

iQGzBAABCgAdFiEE39eBp8gzSXXy/Q3H2jv9z4139nUFAlyzQtkACgkQ2jv9z413
9nX7tQv8DPDjhZAu4gJ2oVlX8emY46T7mBxUG/3mBW1hKdJwDcdNFiw8EhPDspmF
ORjlVGZCtiGHPDoYXkZCTBbNN4+LhE3o7Envoq3wsqLUQ2125Y8jS1UqV6M1Ogpd
oYlYkYONXTHardn8aYZcKIjuZdVvIvUtqEme55Tv2yfDfIR/CJY834Zjpx1eMKND
DqCqgxE1d8Z0ps2o3z774+yxuSY5eWcdzXWn+nZAS/SDTS3TL7SiUPgPfMpWbp+h
hvt9igxrZeBQebJxOOgKtK7kEnJCPFr6EWZ4xXxK5qDUugkS6tF8GztLBO5IqMIz
ebaK7tI6DPpbNQ1cj4OAG36QdSG7l5fCjiB1eomvRUFxFT7BvzUfNLMOd3g6OuOi
bgH/zUH0vVwdCmETO7hnWdFF8vOV3KkE6BfjMVYfa9c4vY0Y4CadowOa9KxW/xxT
U8Ayk9eLPMkoq2b2fqwIxzTgavDl1mbZSnoglNRuYo/5YBHjnhPQXxoJvEX/qCkX
h5DKkpzW
=iXYy
-----END PGP SIGNATURE-----

基于 annotated tag 进行 git describe:

~$ git describe
v1.0

git verify-tag 只能用于校验 annotated tag 的 gpg 签名:

~$ git verify-tag v1.0
gpg: Signature made Sun 14 Apr 2019 10:25:29 PM CST
gpg:                using RSA key DFD781A7C8334975F2FD0DC7DA3BFDCF8D77F675
gpg: Good signature from "luo.runbing (https://www.example.com/) <luo.runbing@example.com>" [ultimate]
gpg:                 aka "runsisi (https://runsisi.com/) <runsisi@hust.edu.cn>" [ultimate]
~$ git verify-commit v1.0
error: v1.0: cannot verify a non-commit object of type tag.
~$ git verify-commit 857f66f
gpg: Signature made Sat 13 Apr 2019 04:04:22 PM CST
gpg:                using RSA key DFD781A7C8334975F2FD0DC7DA3BFDCF8D77F675
gpg: Good signature from "luo.runbing (https://www.example.com/) <luo.runbing@example.com>" [ultimate]
gpg:                 aka "runsisi (https://runsisi.com/) <runsisi@hust.edu.cn>" [ultimate]

参考资料

What is the difference between an annotated and unannotated tag?

https://stackoverflow.com/questions/11514075/what-is-the-difference-between-an-annotated-and-unannotated-tag/11514139

Git: The Difference Between Lightweight and Annotated Tags

https://rem.co/blog/2015/02/12/git-the-difference-between-lightweight-and-annotated-tags/