$ gpg --list-keys
/Users/kody/.gnupg/pubring.gpg
---------------------------------
pub 2048R/0A46826A 2021-09-19
uid Scott Chacon(Git signing key)<kody@gmail.com>
sub 2048R/874529A9 2021-09-19
如果還沒有安裝一個密鑰,可以使用 gpg --gen-key 生成一個:
$ gpg --gen-key
一旦有一個可以簽署的私鑰,可以通過設置 Git 的 user.signingkey 選項來簽署:
$ git config --global user.signingkey 0A46826A
現(xiàn)在 Git 默認使用我們安裝的密鑰來簽署標簽與提交。
二、簽署標簽
如果已經(jīng)設置好一個 GPG 私鑰,可以使用它來簽署新的標簽。所有需要做的只是使用 -s 代替 -a 即可:
$ git tag -s v1.5-m 'my signed 1.5 tag'You need a passphrase to unlock the secret key for
user:"Ben Straub <ben@straub.cc>"2048-bit RSA key, ID 800430EB, created 2021-09-19
如果在那個標簽上運行 git show,會看到 GPG 簽名附屬在后面:
$ git show v1.5
tag v1.5
Tagger: Ben Straub <ben@straub.cc>
Date: Sat May 320:29:412021-0700my signed1.5 tag
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1iQEcBAABAgAGBQJTZbQlAAoJEF0+sviABDDrZbQH/09PfE51KPVPlanr6q1v4/Ut
LQxfojUWiLQdg2ESJItkcuweYg+kc3HCyFejeDIBw9dpXt00rY26p05qrpnG+85b
hM1/PswpPLuBSr+oCIDj5GMC2r2iEKsfv2fJbNW8iWAXVLoWZRF8B0MfqX/YTMbm
ecorc4iXzQu7tupRihslbNkfvfciMnSDeSvzCpWAHl7h8Wj6hhqePmLm9lAYqnKp
8S5B/1SSQuEAjRZgI4IexpZoeKGVDptPHxLLS38fozsyi0QyDyzEgJxcJQVMXxVi
RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk==EFTF
-----END PGP SIGNATURE-----commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <kody@gee-mail.com>
Date: Mon Mar 1721:52:112020-0700changed the version number
三、驗證標簽
要驗證一個簽署的標簽,可以運行 git tag -v ,這個命令使用 GPG 來驗證簽名。為了驗證能正常工作,簽署者的公鑰需要在我們的鑰匙鏈中:
$ git tag -v v1.4.2.1
object 883653babd8ee7ea23e6a5c392bb739348b1eb61
type commit
tag v1.4.2.1
tagger Junio C Hamano <junkio@cox.net>1158138501-0700GIT 1.4.2.1Minor fixes since 1.4.2, including git-mv and git-http with alternates.
gpg: Signature made Wed Sep 1302:08:252020 PDT using DSA key ID F3119B9A
gpg: Good signature from "Junio C Hamano <junkio@cox.net>"
gpg: aka "[jpeg image of size 1513]"
Primary key fingerprint:35652A26 2040 E066 C9A7 4A7D C0C6 D9A4 F311 9B9A
如果沒有簽署者的公鑰,那么將會得到類似下面的東西:
gpg: Signature made Wed Sep 1302:08:252020 PDT using DSA key ID F3119B9A
gpg: Can't check signature: public key not found
error: could not verify the tag 'v1.4.2.1'
$ git commit -a -S -m 'signed commit'You need a passphrase to unlock the secret key for
user:"Scott Chacon (Git signing key) <kody@gmail.com>"2048-bit RSA key, ID 0A46826A, created 2014-06-04[master 5c3386c]signed commit4 files changed,4insertions(+),24deletions(-)rewrite Rakefile(100%)create mode 100644 lib/git.rb
git log 也有一個 --show-signature 選項來查看及驗證這些簽名:
$ git log --show-signature -1
commit 5c3386cf54bba0a33a32da706aa52bc0155503c2
gpg: Signature made Wed Jun 419:49:172020 PDT using RSA key ID 0A46826A
gpg: Good signature from "Scott Chacon (Git signing key) <kody@gmail.com>"
Author: Scott Chacon <kody@gmail.com>
Date: Wed Jun 419:49:172014-0700signed commit
另外,也可以配置 git log 來驗證任何找到的簽名并將它們以 %G? 格式列在輸出中:
$ git log --pretty="format:%h %G? %aN %s"5c3386c G Scott Chacon signed commit
ca82a6d N Scott Chacon changed the version number
085bb3b N Scott Chacon removed unnecessary test code
a11bef0 N Scott Chacon first commit
$ git merge --verify-signatures signed-branch
Commit 13ad65e has a good GPG signature by Scott Chacon(Git signing key)<kody@gmail.com>
Updating 5c3386c..13ad65e
Fast-forwardREADME |2++1 file changed,2insertions(+)
$ git merge --verify-signatures -S signed-branch
Commit 13ad65e has a good GPG signature by Scott Chacon(Git signing key)<kody@gmail.com>You need a passphrase to unlock the secret key for
user:"Scott Chacon (Git signing key) <kody@gmail.com>"2048-bit RSA key, ID 0A46826A, created 2020-06-04Merge made by the 'recursive' strategy.README |2++1 file changed,2insertions(+)