首页 > 其他 > 详细

同时连接gitlab和github

时间:2019-07-30 17:14:21      阅读:87      评论:0      收藏:0      [点我收藏+]

---恢复内容开始---

原文地址:https://juejin.im/post/5ac0cf356fb9a028df22c246

 

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

技术分享图片

这时候可以看到~/.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文件

vim config

添加配置,内容为

# 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! Youve successfully authenticated, but GitHub does not provide shell access.

如果出现以上内容,则代表连接已经成功了,接下来就可以愉快的搞git了

 

注意事项

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

git config --local user.name aaa
git config --local user.email aaa@qq.com

 

如果测试连接失败,Permission denied (publickey).原因是们自定义了 id_rsa_github 钥匙名,默认情况下,连接会搜索 id_rsa 钥匙名,所以这里会失败

 

可以通过以下操作了解连接失败的具体原因

ssh -T -v git@github.com 

 

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

开启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
eval $(ssh-agent -k) ←┘
Agent pid 8428 killed

 

 

如果初始化仓库的时候报以下错误

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

原文:https://www.cnblogs.com/yrxns/p/11271182.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!