Git 设置多个 SSH Key

生成两个ssh密钥

ssh-keygen -t rsa -C 'xxxxx@xxx.com' -f ~/.ssh/id_rsa_key1

ssh-keygen -t rsa -C 'xxxxx@xxx.com' -f ~/.ssh/id_rsa_key2

在 ~/.ssh 目录下config文件添加

Host github1
HostName github.com
User key1
IdentityFile ~/.ssh/id_rsa_key1

Host github2
HostName github.com
User key2
IdentityFile ~/.ssh/id_rsa_key2

ssh 测试

ssh -T git@github.com

**成功返回:**Hi key! You've successfully authenticated, but GitHub does not provide shell access.

在github SSH keys或Deploy keys 添加.pub 公钥

SSH and GPG keys
SSH and GPG keys

在项目目录下设置邮箱和用户名

git config user.name "key1"
git config user.email "xxxxx@xxx.com"

ssh-add ~/.ssh/id_rsa_key1

完结