一台电脑如何连接多个远程仓库

404 阅读1分钟

1前言

有时我们需要连接两个git仓库,比如公司一个gitlab仓库,自己要连github仓库,也就是管理多个ssh key。这就产生配置问题,下面讲讲我的亲测经历。

2生成ssh key

默认我们已经连过一个git仓库(请看:如何连接git远程仓库)。默认的会在~/.ssh/ 目录下生成 id_rsa 和 id_rsa.pub 两个文件。 现在要新增一个仓库,就要多申请一个ssh key。

ssh-keygen -t rsa -C "youremail@yourcompany.com"

为了区分默认的,我们在回车配置时加上名称,

Enter file in which to save the key (/root/.ssh/id_rsa):~/.ssh/自定义文件名

这样就会生成两对rsa及rsa.pub文件。

3配置文件config

在 ~/.ssh 目录下新建一个config文件

touch ~/.ssh/config

添加内容

# github
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
user git

# gitlab
Host gitlab.com
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa
    user git

测试

ssh -T git@github.com

测试是否连接成功。若出现 Hi XXX! You've successfully authenticated, but GitHub does not provide shell access.则代表连接成功!