跳过正文
  1. Posts/

如何创建 GPG 密钥并添加到 GitHub

·307 字·1 分钟· ·
沈显鹏
作者
沈显鹏
DevOps & Build 工程师 | Python 爱好者 | 开源贡献者
目录

1. 查看现有 GPG 密钥
#

# 如果文件夹不存在会自动创建
$ gpg --list-keys
gpg: directory '/home/ubuntu/.gnupg' created
gpg: keybox '/home/ubuntu/.gnupg/pubring.kbx' created
gpg: /home/ubuntu/.gnupg/trustdb.gpg: trustdb created

$ gpg --list-key

2. 生成 GPG 密钥
#

$ gpg --gen-key
gpg (GnuPG) 2.2.19; 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.

Note: Use "gpg --full-generate-key" for a full featured key generation dialog.

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

Real name: shenxianpeng
Email address: xianpeng.shen@gmail.com
You selected this USER-ID:
    "shenxianpeng <xianpeng.shen@gmail.com>"

Change (N)ame, (E)mail, or (O)kay/(Q)uit? O
...
gpg: key 5F72A7D009FC935A marked as ultimately trusted
...
public and secret key created and signed.

pub   rsa3072 2022-07-28 [SC] [expires: 2024-07-27]
      F0F32CB8C65536ECE0187EAD5F72A7D009FC935A
uid                      shenxianpeng <xianpeng.shen@gmail.com>
sub   rsa3072 2022-07-28 [E] [expires: 2024-07-27]

3. 导出公钥内容
#

# 使用邮箱导出
gpg --armor --export xianpeng.shen@gmail.com
# 或使用公钥 ID 导出
gpg --armor --export F0F32CB8C65536ECE0187EAD5F72A7D009FC935A

# 输出为公钥内容

4. 将公钥添加到 GitHub
#

打开 GitHub,进入: Settings → SSH and GPG keys → New GPG key 粘贴刚才导出的公钥内容。

add-gpg-key


完成后,当你使用命令提交:

git commit -S -m "Your commit message"

GitHub 会显示 Verified 签名标识:

verified-signature


转载本文请注明作者与出处,禁止商业用途。欢迎关注公众号「DevOps攻城狮」。

相关文章

Git 常见设置指北

·773 字·2 分钟
本文介绍了在使用 Git 提交代码之前需要进行的一些常见设置,包括配置用户名和邮箱、处理换行符、编写规范的提交信息等,帮助开发者更好地管理代码版本。