一个设备配置多个git key

全文摘自https://www.jianshu.com/p/93f846cef40d 生成key

ssh-keygen -t rsa -C “mail” 在.ssh文件夹添加config

Host github.com #不同点,可更改

  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_1

Host github2.com #不同点

  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_2

清空本地的 SSH 缓存

ssh-add -D

ssh-add ~/.ssh/id_rsa_1

ssh-add ~/.ssh/id_rsa_2 设置git用户名和邮箱

# 取消全局 用户名/邮箱 配置

$ git config –global –unset user.name

$ git config –global –unset user.email

# 进入项目文件夹,单独设置每个repo 用户名/邮箱

$ git config user.email “mail”

$ git config user.name “name”

重建 origin

git remote rm origin

git remote add origin [email protected]:user/xxx.git

ssh 客户端是通过类似 [email protected]:user/xxx.git 的地址来识别使用本地的哪个私钥的,地址中的 User 是@前面的git, Host 是@后面的github.com。