GPG 签名
约 765 字大约 3 分钟
GPGGit
2026-06-04
🔐 为什么使用 GPG 签名
注
GPG签名 能验证 Commit 的身份和完整性。验证成功后,GitHub 会在提交记录中显示 Verified 标识,增强代码的可信度和安全性。
📦 安装 GnuPG
使用 Homebrew 安装。参考 Homebrew 安装教程。
$ brew install gnupg🔑 生成 GPG 密钥
启动生成程序
$ gpg --full-generate-key选择密钥类型
Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) Your selection? 1设置密钥大小
RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (3072) 4096设置有效期
Please specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n years Key is valid for? (0) 1y输入用户信息
Real name: Your Name Email address: [email protected] Comment: (optional - can be left blank)Email必须与GitHub 账号的邮箱一致,否则 GitHub 无法验证签名!设置密码
Enter passphrase: (输入安全密码) Repeat: (重复输入)
🔍 查看密钥
$ gpg --list-secret-keys --keyid-format=long输出示例:
sec rsa4096/3AA5C34371567BD2 2026-06-04 [SC] [expires: 2027-06-04]
85E38EC66371567BD2E7D6C4216A591059929882
uid [ultimate] Your Name <[email protected]>
ssb rsa4096/42B317FD4BA89E7A 2026-06-04 [E] [expires: 2027-06-04]sec 行的 / 后面提取 16 位密钥 ID,如 3AA5C34371567BD2
⚙️ 使用 GPG 签名
1️⃣ 全局启用 GPG 签名
$ git config --global commit.gpgsign true
$ git config --global tag.gpgsign true2️⃣ 配置签名密钥
$ git config --global user.signingkey 3AA5C34371567BD23️⃣ 配置用户名与邮箱
$ git config --global user.name "yourname"
$ git config --global user.email "[email protected]"4️⃣ pinentry-mac Optional
如果想在需要输入 PIN 或 密码短语 时弹出提示,可以先装 pinentry‑mac。
$ brew install pinentry-mac
$ echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
$ killall gpg-agent🚀 导出公钥并上传到 GitHub
🔑 导出公钥
$ gpg --armor --export 3AA5C34371567BD2输出内容:
-----BEGIN PGP PUBLIC KEY BLOCK-----
mI0EZmfQxQEEALzX2hZ3...(中间省略大量字符)...kMQQA
AKCAKQDfw=
-----END PGP PUBLIC KEY BLOCK-----📤 上传到 GitHub
打开 GitHub 设置
添加 GPG 密钥
点击 "New GPG key" 按钮粘贴公钥
将上述完整的公钥内容(从-----BEGIN到-----END的所有内容)粘贴到输入框保存
点击 "Add GPG key" 按钮
✅ 测试 GPG 签名
提交
$ git commit -S -m "Test GPG signature"相关信息
-S参数强制对此提交签名- 如已配置
commit.gpgsign true,可省略-S - 会提示输入 GPG 密码(即生成密钥时设置的密码)
推送
$ git push验证
访问你的仓库,查看提交记录,应该看到
Verified标识(绿色徽章)
📚 参考资源
更新日志
2026/6/8 05:07
查看所有更新日志
f8645-docs(gpg): 📝 add GPG doccumentation于
版权所有
版权归属:Emirio Bomb
