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

环境:

window10 64位
tortoisegit
git
gitee

背景

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

步骤

  1. 下载git和tortoisegit

  2. 在空文件夹右键“Git Bash Here”,如图所示,进去git bash界面。
    在这里插入图片描述

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

    ssh-keygen -t ed25519 -f ~/.ssh/id_rsa_gitee -C “123456@qq.com”

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

    ssh-keygen -t ed25519 -f ~/.ssh/id_rsa_github -C “654321@qq.com”

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

    vim ~/.ssh/config

      # 该文件用于配置私钥对应的服务器
      # first user
      Host gitee.com
      HostName gitee.com
      User 123456@qq.com
      IdentityFile ~/.ssh/id_rsa_gitee
     
      # second user
      Host github.com
      HostName github.com
      User 654321@qq.com
      IdentityFile ~/.ssh/id_rsa_github
    
  5. 将id_rsa_gitee.pub和id_rsa_github.pub分别配置到gitee.com和github.com的ssh公钥配置中。
    在github找到“Settings->SSH and GPG keys”添加id_rsa_github.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_gitee
    ssh-add ~/.ssh/id_rsa_github

    (3)# show all id_rsa

    ssh-add -l

    在这里插入图片描述
    注意:
    a) 如果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

    b) 如果执行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@gitee.com
    ssh -T git@github.com
    在这里插入图片描述

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

注意:(未经测试)
有文章说需要配置不同项目下的用户名和邮箱,在此做一个备注。
需要进入到需要修改的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/

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值