git生成GPG密钥及仅设置github代理

如何生成GPG密钥

注意!先确保已经安装了gpg,windows可以点击这个链接下载

首先先打开cmd/ps/bash

然后运行gpg --full-generate-key

接着命令行会输出以下内容让你输入:

misaka10843@misaka-workstation MINGW64 ~
$ gpg --full-generate-key

gpg (GnuPG) 2.4.7-unknown; Copyright (C) 2024 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.

gpg: directory '/c/Users/misaka10843/.gnupg' created
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? #### 直接回车即可
Please select which elliptic curve you want:
(1) Curve 25519 *default*
(4) NIST P-384
(6) Brainpool P-256
Your selection? #### 直接回车即可
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) #### 直接回车即可
Key does not expire at all
Is this correct? (y/N) y #### 输入y

GnuPG needs to construct a user ID to identify your key.

Real name: #### 输入用户名
Email address: #### 输入邮箱
Comment: #### 输入备注
You selected this USER-ID:
"生成的userid信息"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O #### 输入O

接着会弹出一个窗口让你输入密码,输入即可

在完成之后会输出

public and secret key created and signed.

pub ed25519 2025-08-30 [SC]
BDBDF6FEE85F55025F6BF7F66354F75838AFBD08
uid misaka10843 (github) <misaka10843@xxx>
sub cv25519 2025-08-30 [E]

即生成完成

接着输入gpg --list-secret-keys --keyid-format=long查看生成GPG的信息

misaka10843@misaka-workstation MINGW64 ~
$ gpg --list-secret-keys --keyid-format=long
gpg: checking the trustdb
gpg: marginals needed: 3 completes needed: 1 trust model: pgp
gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
[keyboxd]
---------
sec ed25519/6354F75838AFBD08 2025-08-30 [SC]
BDBDF6FEE85F55025F6BF7F66354F75838AFBD08
uid [ultimate] misaka10843 (github) <misaka10843@xxx>
ssb cv25519/376CD4B40C51362E 2025-08-30 [E]

注意sec的值,如我的示例则记住6354F75838AFBD08这个GPG ID

接着输入gpg --armor --export {GPG ID}即可查看公钥信息

misaka10843@misaka-workstation MINGW64 ~
$ gpg --armor --export 6354F75838AFBD08

-----BEGIN PGP PUBLIC KEY BLOCK-----
xxxxxxxxxxxxxxxxxxxxxxx
-----END PGP PUBLIC KEY BLOCK-----

复制从-----BEGIN PGP PUBLIC KEY BLOCK-----开头-----END PGP PUBLIC KEY BLOCK-----结尾的信息并打开github的SSH and GPG keys -> new GPG key 新建key并填写相关信息后即可

image

最后在命令行中运行git config --global user.signingkey {GPG ID}设置默认使用的签名即可

如果使用多个,则应向键追加感叹号 !

若要将 Git 配置为默认对所有提交和标记进行签名,可以输入以下命令:

git config --global commit.gpgsign true
git config --global tag.gpgSign true

注意!请确保能在本机的cmd/bash运行gpg命令,否则可能会遇见无法签名

error: cannot spawn gpg: No such file or directory
error: gpg failed to sign the data:
(no gpg output)
fatal: failed to write commit object

如何导出GPG备份

运行gpg -a -o public-file.key --export {GPG ID}来导出公钥

运行gpg -a -o private-file.key --export-secret-keys {GPG ID}来导出密钥

需要注意的是两个命令导出的文件都是在当前cmd目录中

导入备份则直接可以使用gpg --import {key文件}不管是公钥还是私钥都可以直接导入

请注意,不要将密钥放在不安全的地方!

如何分发GPG公钥

先在cmd中运行gpg -k后会输出如下

misaka10843@misaka-workstation MINGW64 ~
$ gpg -k
[keyboxd]
---------
pub ed25519 2025-08-30 [SC]
BDBDF6FEE85F55025F6BF7F66354F75838AFBD08
uid [ultimate] misaka10843 (github) <misaka10843@xxx>
sub cv25519 2025-08-30 [E]


记住pub下方的ID(如BDBDF6FEE85F55025F6BF7F66354F75838AFBD08)

然后运行gpg --send-key {ID}即可

misaka10843@misaka-workstation MINGW64 ~
$ gpg --send-key BDBDF6FEE85F55025F6BF7F66354F75838AFBD08
gpg: sending key 6354F75838AFBD08 to hkps://keyserver.ubuntu.com

如何指定只github走代理

直接在命令行中输入下方命令即可

git config --global http.https://github.com.proxy {http代理}
git config --global https.https://github.com.proxy {http代理}