Git多密钥配置

参考的博客

生成密钥

打开git bash

ssh-keygen -t rsa -C "你的邮箱" -f ~/.ssh/id_rsa_gitee
ssh-keygen -t rsa -C "你的邮箱" -f ~/.ssh/id_rsa_github

添加密钥到ssh-agent中

Git默认读取的文件文件名为id_rsa,因此我们需要将生成的密钥添加到ssh-agent中。

# 添加密钥地址到ssh-agent
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitee
# 如果出错,可以使用这条命令,再执行上方代码
ssh-agent bash

编写配置文件

编写config文件(~/.ssh目录下),若没有则自行新建。

# github
Host github.com
HostName github.com
PreferredAuthentications publickey  
IdentityFile ~/.ssh/id_rsa_github  
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey  
IdentityFile ~/.ssh/id_rsa_gitee
# 相关参数
# Host:对识别的模式,进行配置对应的主机名和ssh文件
# HostName : 登录主机的主机名
# PreferredAuthentications :设置登录方式(publickey为使用密钥登录,password为使用密码登录)
# IdentityFile :私钥全路径名

查看密钥并在git/gitee添加公钥

查看生成的公钥

cat ~/.ssh/id_rsa_gitee.pub

复制公钥到git/gitee即可

github同理

最后测试下是否添加成功

ssh -T git@github.com
ssh -T gitee@github.com
原文地址:https://www.cnblogs.com/shouyaya/p/15356041.html