找回密码
 立即注册
首页 业界区 安全 Git使用SSH密钥来签名commit

Git使用SSH密钥来签名commit

裴竹悦 2025-6-1 18:19:28
一、生成SSH密钥

推荐使用 Ed25519 算法(更安全)
  1. ssh-keygen -t ed25519 -C "your_email@example.com"
复制代码
或使用 RSA 算法(兼容性更好)
  1. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
复制代码
二、上传SSH公钥

注:Authentication Key和Signing Key可以使用同一个密钥添加
Github选择签名密钥
1.png

三、配置Git使用SSH密钥

注:公钥文件的位置不能有更改
全局设置
  1. git config --global gpg.format ssh
  2. git config --global user.signingkey <public_key>
复制代码
当前仓库设置
  1. git config gpg.format ssh
  2. git config user.signingkey <public_key>
复制代码
签名

手动签名
  1. git commit -S -m "Your signed commit message"
复制代码
自动签名

全局设置
  1. git config --global commit.gpgsign true
复制代码
当前仓库设置
  1. git config commit.gpgsign true
复制代码
四、设置多个SSH密钥

创建

在路径%USERPROFILE%\.ssh文件夹下创建config文件
手动创建或使用git Bash运行命令
  1. touch config
复制代码
配置
  1. # github
  2. Host github.com
  3. HostName github.com
  4. PreferredAuthentications publickey
  5. IdentityFile ~/.ssh/github_key
  6. # github
  7. Host github2.com
  8. HostName github.com
  9. PreferredAuthentications publickey
  10. IdentityFile ~/.ssh/github2_key
  11. # gitee
  12. Host gitee.com
  13. HostName gitee.com
  14. PreferredAuthentications publickey
  15. IdentityFile ~/.ssh/gitee_key
复制代码
Host 别名,用户命令行使用
HostName 托管平台服务器地址
PreferredAuthentications 权限认证(publickey,password,publickey,keyboardinteractive)
IdentityFile 证书文件位置
测试
  1. ssh -t git@github.com
  2. ssh -t git@github2.com
  3. ssh -t git@gitee.com
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册