本文中使用的密钥为示例密钥,并无实际作用
安装git
打开git bash
在桌面上右键打开菜单,点击
生成gpg密钥
输入指令 gpg --full-generate-key
$ gpg --full-generate-key # 输入生成gpg密钥的指令 gpg (GnuPG) 2.2.29-unknown; Copyright (C) 2021 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. 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) (14) Existing key from card Your selection? # 回车,使用默认值(rsa) RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (3072) 4096 # 此处我手动输入4096,因为git文档中注明 `密钥必须至少是 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) # 回车,使用默认值 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: qwert # 输入名字,此处输入的是qwert Email address: 123@456.com # 输入邮箱,此处输入的是123@456.com Comment: test # 输入注释,此处输入的是test You selected this USER-ID: "qwert (test) <123@456.com>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? # 输入o则确认信息无误,生成密钥;输入q则取消生成并退出
输入o并回车后会跳出弹窗让你输入密码并确认(过短会提示,可以强制使用)
查看已有密钥 gpg --list-secret-keys --keyid-format=long
$ 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: 2 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 2u /c/Users/Administrator/.gnupg/pubring.kbx ----------------------------------------- sec rsa4096/25F10366757A2676 2023-02-13 [SC] 112C798ADD267E3918A0663825F10366757A2676 uid [ultimate] qwert (test) <123@456.com> ssb rsa4096/9DE000ED74E49E6B 2023-02-13 [E]
导出公钥
在git bash中输入 gpg --armor --export 25F10366757A2676
注意,此处最后的数组是你生成的密钥对应的id,也就是上述步骤中sec开头的行,rsa4096/
之后的那一串字符,此处是 25F10366757A2676
导出得到的公钥格式应该是如下格式
$ gpg --armor --export 25F10366757A2676 -----BEGIN PGP PUBLIC KEY BLOCK----- mQINBGPp2UYBEADkaspaZ4gNTpuPQ66WuWrKmDUvxu8t3A0wXq7HKwcKYyluZZPI kiujcHgOc9UBSc8eG34LA7mSQFJDoke9bhpnp1OhaIGSikgUluY6sNTy1J7oLK+W XWrWkQWVIeOOiZri/wB6s5+Ijb9F2f8uAw1SOKj16/cUrsqY8KyWarKlipgYkyGc uFIEV3S7zMJQsU66QwV122Mq0SU6unRyBMcN3kWMz2aRsak7ijtJWGm52b4gqQwi KcLzevo2etv04rOzZAd1xSw4KvUoaIuA2wd9t2Yw8yVTy715FG/TrPKJfI/NcoD3 tFGxi24BSbuStJqUUmoCNfLMnIbkZH1RYja6Wwzsr6Jy28jj6veD5ETZsshurUXp qc27F/SiotOzz1BbbncAkYDyBiaQqKELEZ2h6ovgK1kBODHMgMx0EZVBrrxrp5Jq f37fdeiTeegoUPiD5crJyM7OT3K+6AZi5OjMQ7tomMRULoUIsqWMUtMrDzCcmbab geRzrx6xc0LcVQspibU/wHyqvnXa1EnSNA+iL+goZIFr5/0sOvZD+BfjOwjiEKy0 1zXZvErjC7h9nwCpPoetqGjIhgDumJZDaWIASB62f1GeNVd7dpFtVlX8sBz6JlVC kbyoW8uch2ZHTtraMhkt0YjQr4gUj5l93m5J9n2iWkwZqDZCZrYGviWYcQARAQAB tBpxd2VydCAodGVzdCkgPDEyM0A0NTYuY29tPokCTgQTAQgAOBYhBBEseYrdJn45 GKBmOCXxA2Z1eiZ2BQJj6dlGAhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAAAoJ ECXxA2Z1eiZ2Fy0QANm4kZiWwz28enOCcy6HpOwZSoRT2b0qLgeBDqMpACkeZC+7 iQvSwarNMhm/kg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设置 https://github.com/settings/keys
添加新gpg密钥 https://github.com/settings/gpg/new
打开添加新gpg密钥界面并填入title和key
title可以写一个名字,用于区分不同的公钥
key写上面步骤中导出的公钥
按照上述生成的内容填充后应该是如下格式
点击下方的Add GPG key
将此gpg密钥添加到git中
设置git在提交中启用gpg签名
开启commit使用gpg签名 git config --global commit.gpgsign true
将 Git 配置为默认对所有提交进行签名 git config --global user.signingkey 25F10366757A2676
设置之后idea若不开启使用gpg签名则会提交失败
设置idea在提交时使用gpg签名
在idea设置中找到版本控制 > git
,点击配置GPG密钥
,勾选使用GPG密钥签署提交
并在下方选择框中选择使用的密钥
之后提交推送时会弹窗提示输入密码
删除gpg密钥
删除密钥有三个选项:--delete-keys
,--delete-secret-keys
和--delete-secret-and-public-key
--delete-key
s表示从公钥钥匙圈上删除密钥,也就是一同删除公钥和对应的私钥。在分批模式(batch mode)下,密钥必须使用指纹表示,或者使用–yes选项。--delete-secret-key
s表示从私钥钥匙圈上删除密钥。--delete-secret-and-public-key
和--delete-keys
一样,但是如果私钥存在的话,会先移除私钥。在分批模式下,密钥必须使用指纹表示。
此处在git bash中输入 gpg --delete-secret-and-public-key 25F10366757A2676
注意,此处最后的数组是你生成的密钥对应的id,也就是上述步骤中sec开头的行,rsa4096/
之后的那一串字符,此处是 25F10366757A2676
$ gpg --delete-secret-and-public-key 25F10366757A2676 gpg (GnuPG) 2.2.29-unknown; Copyright (C) 2021 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. sec rsa4096/25F10366757A2676 2023-02-13 qwert (test) <123@456.com> Delete this key from the keyring? (y/N) y # 输入y并回车 This is a secret key! - really delete? (y/N) y # 输入y并回车 pub rsa4096/25F10366757A2676 2023-02-13 qwert (test) <123@456.com> Delete this key from the keyring? (y/N) y # 输入y并回车 # 弹窗验证