GPG
是一种加密算法,github
提交时支持使用 GPG
加密(或称签名),来保证你的 commit
在传输的过程中没有被篡改。下面结合 keybase
介绍如何使用 GPG
介绍
Github 配置 GPG
配置 GPG 公钥到仓库
Github Setting
-> SSH and GPG keys
-> New GPG Key
导入即可
本地代码仓库启用GPG Sign
通过gpg --list-keys
查看pub GPG key ID
,后设置git
签名时用的key
全局设置
# 配置已经生成的GPG Key ID
git config --global user.signingkey <pub GPG key ID>
# 配置启用GPG签名
git config --global commit.gpgsign true
指定仓库设置,需要进入代码目录:
# 配置已经生成的GPG Key ID
git config --local user.signingkey <pub GPG key ID>
# 配置启用GPG签名
git config --local commit.gpgsign true
重启 gpg-agent
第一次配置,必须重启,否则签名会失败,命令如下:
gpgconf –kill gpg-agent
上述步骤示例
~ xiexianbin$ git config --global user.signingkey 11518af49eaa27d86ea01b5c901487ea218aeb1a
~ xiexianbin$ git config --global commit.gpgsign true
~ xiexianbin$ gpgconf –kill gpg-agent
gpg:OpenPGP:/usr/local/Cellar/gnupg/2.2.15/bin/gpg
gpg-agent:私钥:/usr/local/Cellar/gnupg/2.2.15/bin/gpg-agent
scdaemon:智能卡:/usr/local/Cellar/gnupg/2.2.15/libexec/scdaemon
gpgsm:S/MIME:/usr/local/Cellar/gnupg/2.2.15/bin/gpgsm
dirmngr:网络:/usr/local/Cellar/gnupg/2.2.15/bin/dirmngr
pinentry:密码条目:/usr/local/opt/pinentry/bin/pinentry
~ xiexianbin$
关闭GPG签名
所有仓库:
git config --global commit.gpgsign false
git config --global --unset commit.gpgsign
本地仓库:
git config --local commit.gpgsign false
git config --local --unset commit.gpgsign
git 使用
提交
git commit -am "feature: something"
git push origin develop
然后我们可以在 git 中看到 Verified
的标识。
如果不设置git config --global commit.gpgsign true
,提交的时候加上一个 -S
参数就可以为提交签名:
git commit -S -m `your commit message`
提交tag时签名
git tag -s ...
查看日志
git log --show-signature -1
错误
错误一
$ GIT_TRACE=1 git commit -m "xxx"
22:03:55.417251 git.c:455 trace: built-in: git commit -m 'feature: jwt support'
22:03:55.424110 run-command.c:667 trace: run_command: gpg --status-fd=2 -bsau B5A1B728A2FD170FE0E6C4E2D6B71988603A67D2
error: gpg failed to sign the data
fatal: failed to write commit object
或
error: gpg 无法为数据签名
fatal: 写提交对象失败
echo export GPG_TTY=$(tty) >> ~/.bash_profile
如果没有解决,执行如下命令:
$ gpg --status-fd=2 -bsau B5A1B728A2FD170FE0E6C4E2D6B71988603A67D2
# 如果卡住,执行
killall gpg-agent