在git中使用GPG给tag签名的方法

平台:windows
git:官版 git
gui:smartgit
GPG:gungpg

gungpg默认key存储位置在c:\Users\<用户名>\AppData\Roaming\gnupg\

而git默认访问位置在c:\Users\<用户名>\.gnupg\

所以需要使用mklink创建链接从实际位置指向git访问位置

script
1
mklink /D c:\Users\<用户名>\.gnupg c:\Users\<用户名>\AppData\Roaming\gnupg

git默认使用的签名是与用户名和邮箱对应的签名
可以在库(repository)的.git目录下的配置文件(config)中添加如下内容指定KEY-ID

1
2
[user]
signingkey = <gpg-key-id>

这个方法对多个KEY同样有效 或者可以设置全局KEY-ID

script
1
git config --global user.signingkey <gpg-key-id>

打tag的命令:

script
1
git tag -s <tagname> -m "<tagmessage>" <commitSHA1-id>

另外 也可以使用-u参数指定特定提交使用的KEY-ID

script
1
git tag -u <KEY-ID> -s <tagname> -m "<tagmessage>" <commit-id>

完成后用如下指令可以查看结果

script
1
git show <tagname>

验证tag签名的方法

script
1
git tag -v <tagname>

同样可以签署commit

script
1
git commit -a -S -m "<tagmessage>"

在 Git 1.8.3 及以后的版本中,“git merge” 与“git pull” 可以使用--verify-signatures选项来检查并拒绝没有携带可信 GPG 签名的提交。
如果使用这个选项来合并一个包含未签名或有效的提交的分支时,合并不会生效。

script
1
git merge --verify-signatures non-verify

也可在merge签名

script
1
git merge --verify-signatures -S signed-branch

参考:

git help tag
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001376951758572072ce1dc172b4178b910d31bc7521ee4000 https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E7%AD%BE%E7%BD%B2%E5%B7%A5%E4%BD%9C https://airk000.github.io/git/2013/09/30/git-tag-with-gpg-key
另外是一个签名的“幽默故事” http://www.oschina.net/translate/git-horror-story