# 使用 GPG 密钥签署提交

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

CLion 依赖于 Git 的内置 [GPG commit signing](https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work)功能。 为了获得最佳体验，我们建议使用带有图形界面的  gpg2  软件包来处理密码提示。

## 配置环境

Windows:

Procedure: 设置 GPG 支持

* 请执行以下操作之一：

* 下载并安装最新的 [GitForWindows](https://gitforwindows.org) 版本（您需要版本 2.19.2 或更高版本）。 预配置的 GPG 是该包的一部分。 要验证设置是否正确，请打开 GitBash，运行 `gpgconf` 命令并确保输出如下所示： ``` gpg:OpenGPG:/usr/bin/gpg gpg-agent:Private Keys:/usr/bin/gpg-agent scdaemon:Smartcards:/usr/lib/gnupg/scdaemon gpgsm:S/MIME:/usr/bin/gpgsm dirmngr:Network:/usr/bin/dirmngr pinentry:Passphrase Entry:/usr/bin/pinentry ``` 确保 pinentry 显示 GUI 提示，请运行 `echo GETPIN | pinentry` 命令。

* 下载并安装 [Gpg4Win](https://www.gpg4win.org) 包并确保 `git config gpg.program` 指向该包中的 `gpg.exe` 文件，具体操作如下： 1. 运行 `where.exe gpg` 。 2. 如果输出返回多个可执行文件，请找到来自 Gpg4Win 的那个（默认路径是 `C:\Program Files (x86)\GnuPG\bin\gpg.exe` ）。 3. 运行 `git config --global gpg.program "path/to/gpg/from/Gpg4Win"` 。

macOS:

Procedure: 设置 GPG 支持

* 请执行以下操作之一：

* 下载并安装 [GPGTools](https://gpgtools.org)。 预配置的 GPG 是该包的一部分。 确保 `git config gpg.program` 指向包中的 `gpg` 文件（默认路径为 `/usr/local/MacGPG2/bin/gpg` ）。

* 下载并打开 [Homebrew](https://brew.sh) 并运行以下命令： `brew install gnupg pinentry-mac` 。 要验证一切是否正确设置，请打开终端，运行 `gpgconf` 命令，并确保输出如下： ``` pg:OpenGPG:/usr/local/MacGPG2/bin/gpg gpg-agent:Private Keys:/usr/local/MacGPG2/bin/gpg-agent scdaemon:Smartcards:/usr/local/MacGPG2/libexec/scdaemon gpgsm:S/MIME:/usr/local/MacGPG2/bin/gpgsm dirmngr:Network:/usr/local/MacGPG2/bin/dirmngr pinentry:Passphrase Entry:/usr/local/bin/pinentry ``` 确保 pinentry 显示 GUI 提示，请运行 `echo GETPIN | pinentry-mac` 命令。

Linux:

Procedure: 设置 GPG 支持

1. 使用 您的 Linux 发行版自带的软件包管理器安装  `gpg2` 。 实际的 package 清单会根据您使用的发行版而有所不同，其中最重要的是  `gnupg2` 、  `gnupg-agent`  和一个显示 GUI 提示的  `pinentry` 。

例如，在 Ubuntu/Debian 上，运行  `sudo apt -y install gnupg2 gnupg-agent pinentry-gnome3` 。

2. 要验证所有设置是否正确，请打开终端，运行  `gpgconf` 命令，并确保输出如下所示：

```
gpg:OpenPGP:/usr/bin/gpg
gpg-agent:Private Keys:/usr/bin/gpg-agent
scdaemon:Smartcards:/usr/lib/gnupg/scdaemon
gpgsm:S/MIME:/usr/bin/gpgsm
dirmngr:Network:/usr/bin/dirmngr
pinentry:Passphrase Entry:/usr/bin/pinentry

```

确保使用  `echo GETPIN | pinentry`  命令时，  `pinentry`  显示图形界面提示。

## 设置 GPG 密钥

最安全的方法是使用智能卡，例如 [Yubikey](https://www.yubico.com/products/) 来存储您密钥的私有部分。 如需了解有关设置 Yubikey 的更多信息，请参阅 [YubiKey-Guide](https://github.com/drduh/YubiKey-Guide)。

Procedure: 导入 GPG 密钥

如果您已经有 GPG 密钥，您需要将它们导入到相应的 GPG 密钥环中。

* 打开   终端 / 命令提示符 / GitBash / 您系统上的任何其他 shell  并运行以下命令：  `gpg --import <path to your private gpg.key>`

Procedure: 生成 GPG 密钥

如果尚未有密钥，您需要生成一对新的。

1. 打开   终端 / 命令提示符 / GitBash / 您系统上的任何其他 shell  并运行以下命令：  `gpg --full-generate-key` （对于 pgp 2.1.17 及以下版本，请使用  `gpg --gen-key` 命令）。

2. 回答工具返回的问题。 推荐的选项是：

* 密钥类型 ：RSA

* 密钥大小 ：至少 4096 位

* 密钥有效期 ：1 年（最好每年轮换一次密钥）

3. 请输入您的用户 ID 信息。 建议使用与您的提交中显示的作者相同的用户名和电子邮件地址。 如果您计划将签名与电子邮件地址隐私功能一起使用，请指定 GitHub noreply 电子邮件地址。

4. 请输入一个安全的密码短语。 请确保您是在弹出的对话框中输入它，而不是在命令行中输入，因为对于此类提示，使用 GUI 是很重要的。

5. 请确保通过运行以下命令导入这些 keys：  `gpg --list-keys` 。

## 启用提交签名

在启用提交签名之前，请确保您已 [设置 GPG 密钥](#set-up-gpg-keys)。

Procedure:

1. 启动 CLion（或重新启动以确保加载您对环境所做的更改）。

2. 在   设置   对话框 ( `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `⌘ Comma` (Xcode), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (ReSharper), `⌘ Comma` (ReSharper (macOS)), `Ctrl+Alt+S` (QtCreator), `⌘ Comma` (QtCreator (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS))) 中，转到   版本控制 | Git  ，然后点击   配置 GPG 密钥   按钮。

3. 在打开的对话框中，点击   使用 GPG 密钥签署提交   并从列表中选择您想要使用的密钥。

现在，每次提交都会使用所选密钥进行签名。 GPG 签名的状态将显示在日志选项卡上的提交详情窗格中。

> **Tip:**
> 如果 MacOS 上的 GPG 签名因错误  `fatal: failed to write commit object`  而失败，请按照 [简短的故障排除指南](https://youtrack.jetbrains.com/articles/SUPPORT-A-1234)解决此问题。

## 将 GPG 密钥添加到您的帐户

如果您使用的 Git 托管支持 GPG 签名验证，请上传您的公钥部分。

请遵循以下指示

* [GitHub](https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification/adding-a-new-gpg-key-to-your-github-account)

* [空间](https://www.jetbrains.com/zh-cn/help/space/git-keys-and-passwords.html#gpg-key)

* [GitLab](https://docs.gitlab.com/ee/user/project/repository/signed_commits/gpg.html#add-a-gpg-key-to-your-account)

* [Bitbucket](https://confluence.atlassian.com/bitbucketserver/using-gpg-keys-913477014.html#UsingGPGkeys-AddaGPGkey)

