首页 > 其他 > 详细

一台机器git配置gitlab和github两个账号和邮箱

时间:2021-05-17 15:47:36      阅读:16      评论:0      收藏:0      [点我收藏+]

环境:

window10 64位
tortoisegit
git
gitlab

背景

由于公司使用gitlab部署私有代码库,个人有需求查看github的开源代码,如果配置全局的账号密码会冲突。所以有必要生成两个id_rsa.pub密钥分别配置到github和gitlab的ssh密钥中去。

步骤

  1. 下载git和tortoisegit

  2. 在空文件夹右键“Git Bash Here”,如图所示,进去git bash界面。
    技术分享图片

  3. 设置无密码登录
    a.生成第1个git无密码登录的id_rsa

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_1 -C “第1个邮箱”

    如图所示,直接回车即可
    技术分享图片
    b.生成第2个git无密码登录的id_rsa

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_2 -C “第2个邮箱”

  4. 此时能在~/.ssh/config文件中看到第一个邮箱的配置,添加第二个邮箱配置。

    vim ~/.ssh/config

     # 该文件用于配置私钥对应的服务器
     # first user
     Host git@github.com
     HostName https://github.com
     User 用户名
     IdentityFile ~/.ssh/id_1
    

    second user

    Host git@gitlab.10101111.com
    HostName http://gitlab.10101111.com
    User 用户名
    IdentityFile ~/.ssh/id_rsa_2

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  5. 将id_rsa_1.pub和id_rsa_2.pub分别配置到github.com和gitlab.10101111.com的ssh密钥配置中。
    在github找到“Settings->SSH and GPG keys”添加id_rsa_1.pub;在gitlab.10101111.com找到“设置->SSH密钥”添加id_rsa_2.pub。
    技术分享图片

  6. 取消git的全局配置(如果之前就有配置的情况下)

    git config --global --unset user.name “XXX”
    git config --global --unset user.email "xxx@xx.com"

    查看是否取消成功:

    git config --global -l

    如图下图所示,如果没有用户名和邮箱就代表取消了全局配置了。
    技术分享图片

  7. 执行ssh识别
    (1)#Start the ‘ssh-agent.exe’ process

    eval $(ssh-agent -s)

    (2)#install the SSH keys

    ssh-add ~/.ssh/id_rsa_1
    ssh-add ~/.ssh/id_rsa_2

    (3)# show all id_rsa

    ssh-add -l

    技术分享图片
    注意:
    1、如果ssh-add这步报错:Could not open a connection to your authentication agent.
    需要先启动ssh-agent,也就是上面的第一个步骤。
    参考链接:https://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent
    2、如果执行ssh-add -l报错:Could not open a connection to your authentication agent.
    技术分享图片
    需要先将密钥添加,也就是上述的第二步。
    参考链接:https://stackoverflow.com/questions/26505980/github-permission-denied-ssh-add-agent-has-no-identities

  8. 测试是否能无密码连接成功

    ssh -T git@gitlab.10101111.com
    ssh -T git@github.com

    技术分享图片

  9. 此时可以克隆代码到本地了。

    git clone git@github.com:grcpeng/spring-boot.git

    技术分享图片
    注意:(未经测试)
    有文章说需要配置不同项目下的用户名和邮箱,在此做一个备注。
    需要进入到需要修改的git工程目录中

    git config user.name 用户名
    git config user.email 邮箱

参考链接:

1、https://blog.csdn.net/qq1332479771/article/details/70149616
2、https://blog.csdn.net/jifaliwo123/article/details/79126785
3、http://www.xuanfengge.com/using-ssh-key-link-github-photo-tour.html
4、https://gist.github.com/yeungeek/596984fd9e53d6c36c0d
5、https://favoorr.github.io/2015/05/27/git-more-sshkeys-more-host/

一台机器git配置gitlab和github两个账号和邮箱

原文:https://www.cnblogs.com/yangxd1994/p/14776844.html

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