配置 GPG
安装
我这里使用 scoop 来管理常用的软件。
|
|
查看安装的版本
|
|
结果如下:
|
|
生成新的 GPG 密钥
-
输入如下命令:
1
gpg --full-generate-key
-
上面的命令执行之后,会要求选用对应的加密算法。我们在这里直接回车选择默认的 9 使用 ECC 椭圆曲线加密算法用于加密和解密。提示内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
gpg (GnuPG) 2.4.3; Copyright (C) 2023 g10 Code GmbH This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) RSA and RSA (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (9) ECC (sign and encrypt) *default* (10) ECC (sign only) (14) Existing key from card Your selection?
-
选择完成之后,会提示选用对应的椭圆曲线。我们选择默认的
Curve 25519
椭圆曲线。提示内容如下:1 2 3 4
Please select which elliptic curve you want: (1) Curve 25519 *default* (4) NIST P-384 (6) Brainpool P-256
-
设置过期时间,根据需要设置,我这里选的是 0,设置不过期。
1 2 3 4 5 6 7
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)
-
过期时间选择之后,会提示确认上述的操作,直接输入
y
确认。 -
设置相关的用户标识。上一步确认之后,会提示输入用户名,邮箱和注释这三个内容,按自己的情况输入即可。输入并确认后 GPG 密钥则生成。
1 2 3 4 5 6 7
GnuPG needs to construct a user ID to identify your key. Real name: name Email address: name@example.com Comment: You selected this USER-ID: "name <name@example.com>"
查看已经生成的密钥
|
|
显示如下(相关内容已经过处理):
|
|
导出公钥
这里公钥的导出以上面的 5D4C3FXXXXXX2034EF
为例,命令如下:
|
|
显示的公钥格式如下(中间的具体内容以 ···
代替):
|
|
配置 Git 提交时签名
Github 配置密钥
-
我们先导出当前要使用的公钥
-
点击 Github 右上角的头像,点击
Settings
选项。 -
在左侧的页面单击
SSH and GPG keys
选项。 -
在
GPG keys
旁边,点击New GPG key
按钮。 -
在新页中的
Title
输入自己的备注信息,在Key
中填入第一步中导出的公钥。 -
点击
Add key
按钮,公钥添加成功。
本地配置
-
配置本地 Git 签名使用的密钥 ID,我们这里使用的密钥 ID 为:
5D4C3FXXXXXX2034EF
(与配置在 Github 里面的保持一致)。1 2
# 这里添加 --global 表示设置为全局 git config --global user.signingkey 5D4C3FXXXXXX2034EF
-
配置默认对所有提交进行签名,命令如下:
1
git config --global commit.gpgsign true
-
可以使用
--unset
取消指定的签名 ID。如下:1
git config --global --unset user.signingkey