GitHub 官宣正式支持显示 SSH 签名,因此你可以使用自己生成的 SSH 密钥在本地签名提交记录以防止他人冒充伪造提交记录
前提
生成 SSH 密钥
ssh-keygen -t ed25519 -C "your_email@example.com"
clip < ~/.ssh/id_ed25519.pub # 复制公钥
将结果添加到 GitHub 的 SSH and GPG keys 中。
将 SSH 密钥添加到 ssh-agent
确保 ssh-agent 正在运行
eval "$(ssh-agent -s)"
将 SSH 私钥添加到 ssh-agent
ssh-add ~/.ssh/id_ed25519
使用 SSH 签名
- 全局使用 SSH 签名
git config --global gpg.format ssh
- 指定 SSH 签名使用文件
git config --global user.signingKey ~/.ssh/id_ed25519.pub
- 在 .ssh 目录新建一个可信公钥列表文件 allowed_signers,内容为 id_ed25519.pub 前面加上邮箱
touch ~/.ssh/allowed_signers && clip < ~/.ssh/id_ed25519.pub
echo "your_email@example.com 粘贴到这" >> ~/.ssh/allowed_signers
例如我的列表文件allowed_signers
tobychung@duck.com ssh-ed25519 AAAA(...已省略)RuQj tobychung@duck.com
- 指定可信公钥列表文件
git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers
- 开启全局自动签名
git config --global commit.gpgsign true git config --global tag.gpgsign true
测试现有仓库
git commit --allow-empty --message="Testing SSH signing"
查看签名
git log --show-signature -3
这个时候我们就可以看到第一条有
Good "git" signatures ...
验证消息了。认证标识
- 勾选账号设置 SSH and GPG keys 中的 Vigilant mode

- 再次添加相同的公钥重点 Key type 要选择 signing key 即可。
clip < ~/.ssh/id_ed25519.pub
