2019-04_Git实现github和gitlab账号SSH免密配置

1、 生成秘钥
$ cd ~/.ssh/
# 命令输入完,需自定义命名为:id_rsa_gitlab和id_rsa_github
ssh-keygen -t rsa -C "33999***@qq.com"
ssh-keygen -t rsa -C "33999***@company.com"
2、添加秘钥
# 把专用密钥添加到 ssh-agent 的高速缓存中: 
ssh-add ~/.ssh/id_rsa_gitlab
ssh-add ~/.ssh/id_rsa_github

# 从ssh-agent中删除密钥: 
ssh-add -d ~/.ssh/id_rsa_gitlab.pub
ssh-add -d ~/.ssh/id_rsa_github.pub

# 查看ssh-agent中的密钥: 
ssh-add -l

注:如果在使用shh-add的时候提示:
Could not open a connection to your authentication agent.
则需手动开启ssh,如下;

ssh-agent bash
3、 将gitlab公钥即id_rsa.pub中的内容配置到gitlab和gitbub
4、 进入密钥生成的位置,创建一个config文件,添加配置
$ cd ~/.ssh/
# gitlab(1****64@qq.com)
    Host gitlab.com
  HostName gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_gitlab
  User lvtong

# github(j****4@163.com)
    Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_github
  User jackaroo2020
4、测试
$ cd ~/.ssh/
# $ ssh -T git@github.com
$ ssh -T git@gitlab.com
5. 用户级别配置
  • 公司的代码使用频率较高,将git配置文件的global(用户级别)设置为公司的gitlab账号
$ git config --global user.name 'test' #公司账号名称
$ git config --global user.email 'test@companyName.com' #公司账号邮箱
  • 仓库级别配置,将local(仓库级别)配置成github的账号
$ git config --local user.name 'test' #github账号名称
$ git config --local user.email 'test@test.com' #github账号邮箱
  • 克隆代码
$ git clone git@github.com:test/demo.git
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值