使用 GPG 簽名 Git Commit
GPG 安裝
bash
brew install gpg
# or
brew cask install keybase常用命令
bash
$ gpg --gen-key
$ gpg --list-keys
$ gpg --list-secret-keys
$ gpg2 --list-keys
$ gpg2 --list-secret-keys
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys C6EED57A
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys C6EED57A生成 GPG key
bash
gpg --gen-key具體步驟如下
bash
$ gpg --gen-key
gpg (GnuPG) 2.2.15; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
注意:使用 “gpg --full-generate-key” 以獲得一個功能完整的密鑰產生對話框。
GnuPG 需要構建用戶標識以辨認您的密鑰。
真實姓名: Theo
電子郵件地址: fanxiaobin422@gmail.com
您選定了此用戶標識:
“Theo <fanxiaobin422@gmail.com>”
更改姓名(N)、註釋(C)、電子郵件地址(E)或確定(O)/退出(Q)? o
我們需要生成大量的隨機字節。在質數生成期間做些其他操作(敲打鍵盤
、移動鼠標、讀寫硬盤之類的)將會是一個不錯的主意;這會讓隨機數
發生器有更好的機會獲得足夠的熵。
gpg: 密鑰 8284896418BCC645 被標記為絕對信任
gpg: 目錄‘/Users/xiexianbin/.gnupg/openpgp-revocs.d’已創建
gpg: 吊銷證書已被存儲為‘/Users/xiexianbin/.gnupg/openpgp-revocs.d/11518af49eaa27d86ea01b5c901487ea218aeb1a.rev’
公鑰和私鑰已經生成並被簽名。
pub rsa2048 2019-05-16 [SC] [有效至:2021-05-15]
11518af49eaa27d86ea01b5c901487ea218aeb1a
uid xiexianbin <me@xiexianbin.cn>
sub rsa2048 2019-05-16 [E] [有效至:2021-05-15]查看 GPG key
bash
gpg --list-keys
gpg --list-secret-keys示例
bash
$ gpg --list-keys
/Users/xiexianbin/.gnupg/pubring.kbx
------------------------------------
pub rsa2048 2019-05-16 [SC] [有效至:2021-05-15]
11518af49eaa27d86ea01b5c901487ea218aeb1a
uid [ 絕對 ] xiexianbin <me@xiexianbin.cn>
sub rsa2048 2019-05-16 [E] [有效至:2021-05-15]密鑰
pub:公鑰sub:私鑰11518af49eaa27d86ea01b5c901487ea218aeb1a是 pub GPG key ID
導出公鑰
bash
gpg --armor --export <pub GPG key ID>GPG key 格式
bash
-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----刪除GPG key
bash
gpg --delete-keys <pub GPG key ID>
gpg --delete-secret-keys <pub GPG key ID>Github 配置 GPG
配置 GPG 公鑰到倉庫
Github Setting -> SSH and GPG keys -> New GPG Key 導入即可
本地代碼倉庫啟用GPG Sign
通過 gpg --list-keys 查看 pub GPG key ID,後設置 git簽名 時用的 key
全局設置
bash
# 配置已經生成的GPG Key ID
git config --global user.signingkey <pub GPG key ID>
# 配置啟用GPG簽名
git config --global commit.gpgsign true指定倉庫設置,需要進入代碼目錄:
bash
# 配置已經生成的GPG Key ID
git config --local user.signingkey <pub GPG key ID>
# 配置啟用GPG簽名
git config --local commit.gpgsign true重啟 gpg-agent
bash
gpgconf –kill gpg-agent上述示例
bash
~ 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$git 使用
提交
bash
git commit -am "feature: something"
git push origin develop然後我們可以在 git 中看到 Verified 的標識。
如果不設置 git config --global commit.gpgsign true,提交的時候加上一個 -S 參數就可以為提交簽名:
bash
git commit -S -m `your commit message`提交 tag 時簽名
bash
git tag -s ...查看日誌
bash
git log --show-signature -1常見問題
bash
$ 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: 寫提交對象失敗解決辦法
bash
echo export GPG_TTY=$(tty) >> ~/.bash_profile
export GPG_TTY=$(tty)重新執行,發現會彈出一個密碼輸入界面。
如果沒有解決,執行如下命令
bash
$ gpg --status-fd=2 -bsau B5A1B728A2FD170FE0E6C4E2D6B71988603A67D2
# 如果卡住,執行
killall gpg-agent