检查现有 GPG 密钥
-
cli 工具安装
$ brew install gnupg
在生成 GPG 密钥之前,您可以检查是否有任何现有的 GPG 密钥。
-
打开 Terminal(终端)。
-
使用
gpg --list-secret-keys --keyid-format=long命令列出您拥有其公钥和私钥的长形式 GPG 密钥。 签名提交或标记需要私钥。$ gpg --list-secret-keys --keyid-format=long
查命令输出以查看是否有 GPG 密钥对。
-
如果没有 GPG 密钥对,或者您不想使用任何可用于签名提交和标记的密钥对,则生成新的 GPG 密钥。
-
如果存在现有的 GPG 密钥对,并且您希望使用它来对提交和标记进行签名,则可以使用以下命令显示公钥,并替换要使用的 GPG 密钥 ID。 在此例中,GPG 密钥 ID 是
3AA5C34371567BD2:$ gpg --armor --export 3AA5C34371567BD2 # Prints the GPG key ID, in ASCII armor format -----BEGIN PGP PUBLIC KEY BLOCK----- xxxxx -----END PGP PUBLIC KEY BLOCK------ 复制 GPG 密钥,从
-----BEGIN PGP PUBLIC KEY BLOCK-----开始,到-----END PGP PUBLIC KEY BLOCK-----结束。
- 复制 GPG 密钥,从
生成 GPG 密钥
gpg --full-generate-key
#跟随提示下一步
使用 gpg --list-secret-keys --keyid-format=long 命令列出您拥有其公钥和私钥的长形式 GPG 密钥。 签名提交或标记需要私钥。
gpg --list-secret-keys --keyid-format=long
- 从 GPG 密钥列表中复制您想要使用的 GPG 密钥 ID 的长形式。 在此例中,GPG 密钥 ID 是
3AA5C34371567BD2:
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot
ssb 4096R/42B317FD4BA89E7A 2016-03-10
```
-
粘贴下面的文本(替换为您要使用的 GPG 密钥 ID)。 在此例中,GPG 密钥 ID 是
3AA5C34371567BD2:$ gpg --armor --export 3AA5C34371567BD2 # Prints the GPG key ID, in ASCII armor format -
复制 GPG 密钥,从
-----BEGIN PGP PUBLIC KEY BLOCK-----开始,到-----END PGP PUBLIC KEY BLOCK-----结束。
配置git config 信息
```
git config --global user.signingkey <your key>
# 自动开启验证
git config commit.gpgsign true
```
commit 报错
添加export GPG_TTY=$(tty) 到配置文件
-
如果您使用的不是 GPG 套件, 在
zshshell 中运行以下命令将GPG 密钥添加到您的shrc文件或.zprofile文件(如果存在):$ if [ -r ~/.zshrc ]; then echo 'export GPG_TTY=$(tty)' >> ~/.zshrc; \ else echo 'export GPG_TTY=$(tty)' >> ~/.zprofile; fi或者,如果您使用
bashshell,则运行皮命令:$ if [ -r ~/.bash_profile ]; then echo 'export GPG_TTY=$(tty)' >> ~/.bash_profile; \ else echo 'export GPG_TTY=$(tty)' >> ~/.profile; fi