同时链接gitlab和github

平时本身会用到github来管理本身的一些项目,同时公司也在用gitlab作版本管理,那么如何在一台电脑上同时链接gitlab和github呢?下面展现一下配置的步骤node

1. 分别生成gitlab和github的ssh key

ssh-keygen -t rsa -C "your.email@example.com" -b 4096
复制代码

生成第一个gitlab的ssh key一路回车便可,生成第二个github的ssh key需注意一下ssh key的名字须要修改一下,不然就会覆盖前面生成的gitlab的key,这里我修改为id_rsa_github git

image
这时候能够看到~/.ssh/目录下生成了如下文件

- id_rsa
- id_rsa.pub 
- id_rsa_github
- id_rsa_github.pub
复制代码

2. 分别复制公钥中的内容,在gitlab和github中添加ssh key

cat ~/.ssh/id_rsa.pub
复制代码

3. 添加config文件

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

vim config
复制代码

添加配置,内容为shell

# gitlab
Host gitlab
    User git
    HostName gitlab.cheanjiait.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

# github
Host github
    User git
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
复制代码

4.测试链接

$ ssh -T gitlab
Welcome to GitLab, Ethan Chen!
复制代码
$ ssh -T github
Hi azumia! You've successfully authenticated, but GitHub does not provide shell access. 复制代码

若是出现以上内容,则表明链接已经成功了,接下来就能够愉快的搞git了vim

注意事项

在使用github时,在项目下初始化git的时候记得定义好user.name和user.emailsegmentfault

git config --local user.name 'aaa'
git config --local user.email 'aaa@qq.com'
复制代码

若是测试链接失败,Permission denied (publickey).缘由是们自定义了 id_rsa_github 钥匙名,默认状况下,链接会搜索 id_rsa 钥匙名,因此这里会失败bash

能够经过如下操做了解链接失败的具体缘由ssh

ssh -T -v git@github.com 
复制代码

针对这个问题的解决方案以下ide

开启ssh-agent

# 开启 agent
eval $(ssh-agent -s) ←┘
Agent pid 8428
复制代码
# 添加 钥匙名
ssh-add ~/.ssh/id_rsa_github ←┘
Identity added: /c/Users/user/.ssh/id_rsa_github (/c/Users/user/.ssh/id_rsa_github)
复制代码
# 查看 agent list
ssh-add -l ←┘
8428 SHA256:A9UcJMadwTpF7TvsT5LEXsSssTs5qehmV9Q2Q8Ntw3o /c/Users/user/.ssh/id_rsa_github (RSA)
复制代码
# 不用时能够关闭 agent
eval $(ssh-agent -k) ←┘
Agent pid 8428 killed
复制代码

若是初始化仓库的时候报如下错误gitlab

ssh: Could not resolve hostname https: nodename nor servname provided, or not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
复制代码

则查看一下本身的git的配置

$ git remote -v
复制代码

将git地址由ssh方式改成https方式便可

参考文档: 同时使用:gitlab & github

相关文章
相关标签/搜索