基本流程参照:https://spencerwoo.com/blog/wait-this-is-not-my-commit

(躺在我的收藏夹里,每次重装电脑就点出来)

首先用 PowerShell 安装 scoop:https://scoop.sh/

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

两句缺一不可,第一句是设置权限。

然后使用 scoop 安装 gpg:

scoop install gpg

装完之后使用 gpg --version 查看 home 目录:

PS C:\Users\TigerBeanst> gpg --version
gpg (GnuPG) 2.3.6
libgcrypt 1.10.1
Copyright (C) 2021 g10 Code GmbH
License GNU GPL-3.0-or-later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: C:\Users\TigerBeanst\scoop\apps\gpg\current\home
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
AEAD: EAX, OCB
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

可得 C:\Users\TigerBeanst\scoop\apps\gpg\current\home 为 home 路径,把已经备份的 gpg 密钥扔进去:

生成密钥就不说了,可以参照原文,这篇主要是记录恢复已有的 GPG 密钥。

将密钥文件扔进 home 目录后,使用下面的指令来查看私钥 ID

gpg --list-secret-keys --keyid-format LONG
PS C:\Users\TigerBeanst> gpg --list-secret-keys --keyid-format LONG
C:\Users\TigerBeanst\scoop\apps\gpg\current\home\pubring.kbx
------------------------------------------------------------
sec   rsa4096/AAAAAAAAAAAAAA 2020-11-11 [SC]
      BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
uid                 [ultimate] XXX(YYY) <Z@example.com>
ssb   rsa4096/CCCCCCCCCCCC 2020-11-11 [E]

此时,AAAAAAAAAAAAAA 就是你的私钥 ID。

进到任意一个 git 项目下(即根目录里有 .git),执行:

git config --global user.signingkey AAAAAAAAAAAAAA
git config --global commit.gpgsign true

是的,我发现我每次重装电脑后都会遇到这个坑……

gpg: signing failed: No secret key

gpg failed to sign the data

failed to write commit object

赤裸裸地摆在 Android Studio 的右下角……

其实只要在任一 git 项目下执行(自行替换相关):

git config --global gpg.program "C:\Users\用户名\scoop\apps\gpg\版本号\bin\gpg.exe"

原因是 Git 默认会去找自己的 gpg,而不是你装的 gpg,你要强制指定。