zoukankan      html  css  js  c++  java
  • 同时连接gitlab和github

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

    原文地址: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

    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文件

    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! You've 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方式即可

  • 相关阅读:
    day10servlet编程
    day9http协议
    day8 服务器
    day4 DOM,BOM
    k均值
    asp.net mvc 下拉列表
    asp.net mvc  Ajax.BeginForm 异步上传图片的问题
    sqlserver 存储过程 游标实例
    中篇: php 微信支付 基于Thinkphp3.2开发
    下篇: php 微商城 基于Thinkphp3.2框架开发
  • 原文地址:https://www.cnblogs.com/yrxns/p/11271182.html
Copyright © 2011-2022 走看看