SSH配置多个Git账号

118 阅读1分钟

一台电脑连接两个git账号,HttpStatusError和HttpStatusOK

使用Git Bash创建SSH密钥

进入目录

cd ~/.ssh

执行命令,创建第一个账号 HttpStatusError 的密钥

ssh-keygen -t rsa -f ~/.ssh/HttpStatusError -C "account1@gmail.com"

创建第二个账号 HttpStatusOK 的密钥

ssh-keygen -t rsa -f ~/.ssh/HttpStatusOK -C "account2@gmail.com"

建立配置文件

~/.ssh 文件夹内创建配置文件 config

touch config

编辑以下代码

Host HttpStatusError
HostName github.com
User git
IdentityFile ~/.ssh/HttpStatusError

Host HttpStatusOK
HostName github.com
User git
IdentityFile ~/.ssh/HttpStatusOK

GitHub配置公钥

分别登录两个账号的GitHub,点击 Settings -> SSH and GPG keys -> New SSH key

添加对应的 HttpStatusError.pub / HttpStatusOK.pub 公钥文件内容

添加后测试

ssh -T HttpStatusError
Hi HttpStatusError! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T HttpStatusOK
Hi HttpStatusOK! You've successfully authenticated, but GitHub does not provide shell access.

克隆项目

注意:克隆的链接要做些修改

// 原本
git clone git@github.com:HttpStatusError/project1.git
git clone git@github.com:HttpStatusOK/project2.git

// 更改为(github.com 部分更改为 config 配置文件下 Host 的定义即刻)
git clone git@HttpStatusError:HttpStatusError/project1.git
git clone git@HttpStatusOK:HttpStatusOK/project1.git