Windows上使用SSH签名Git提交记录

date
Aug 25, 2022
slug
signing-git-commits-with-ssh-key
status
Published
tags
GitHub
Git
SSH
Signature
summary
GitHub前天官宣正式支持SSH签名,因此你可以使用自己生成的SSH密钥在本地签名提交记录以防止他人冒充伪造提交记录。
type
Post

前提GitHub配置SSH

git config --global user.name "tractortoby"
git config --global user.email "tobychung1993@gmail.com"
ssh-keygen -t ed25519 -C "tobychung1993@gmail.com"
clip < ~/.ssh/id_ed25519.pub
将结果添加到GitHub的SSH and GPG keys中。
 
  • 将 SSH 密钥添加到 ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Windows上使用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 "tobychung1993@gmail.com ssh-ed25519 AA(..)sf tobychung1993@gmail.com" >> ~/.ssh/allowed_signers

# 指定可信公钥列表文件
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 -1

打开GitHub认证标识

  • 点击账号设置Settings
  • 点击SSH and GPG keys
  • 勾选Vigilant mode
notion image
  • 再次添加相同的公钥重点Key type要选择signing key即可。
    • clip < ~/.ssh/id_ed25519.pub
      notion image
      notion image

参考

 

© Toby Chung 2021 - 2022