Gitgitee/github/gitlab账号分离

场景:

公司提供的账号维护gitlab中的代码;自己的账户维护在githubgitee中的代码;

步骤一

分别对gitlab、github、gitee生成独立的SSH

#进入 C:\Users\Administrator 打开gitbash进行以下配置

#gitlab:
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "公司提供的邮箱"

#github:
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "私人的邮箱"

#gitee:
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "私人的邮箱"

建一个config文件(这个文件没有后缀)来控制三个SSH

$ touch ~/.ssh/config 

config:

#gitHub
    Host github.com
    HostName github.com
    User yangjing
    IdentityFile ~/.ssh/id_rsa.github

#gitLab 
    Host git@gitlab.com
    HostName gitlab.com
    User yangjing
    IdentityFile ~/.ssh/id_rsa.gitlab

# gitee
    Host gitee.com
    HostName gitee.com
    User yangjing
    IdentityFile ~/.ssh/id_rsa.gitee

配置解释:

  • Host
    它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。
    这里可以使用任意字段或通配符。
    当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。
  • Port
    自定义的端口。默认为22,可不配置
  • User
    自定义的用户名,默认为git,可不配置
  • HostName
    真正连接的服务器地址
  • PreferredAuthentications
    指定优先使用哪种方式验证,支持密码和秘钥验证方式
  • IdentityFile
    指定本次连接使用的密钥文件

步骤二

关联远程仓库的用户和邮箱

这里公司项目(gitlab)使用全局配置

$ git config --global user.name "yangjing"

$ git config --global user.email "公司提供的个人邮箱"

个人项目(gitee/github)在项目单独配置

$ git config user.name "yangjing"

$ git config user.email "私人邮箱"

步骤三

gitlab/gitee/github的个人设置中找到配置SSH的地方,复制 ~/.ssh/id_rsa.xxx.pub 文件内容粘贴到配置sshkey的地方即可;

以gitee为例:

步骤四

测试一下

# 三个代码商都有自己独特的后缀

#测试gitlab
ssh -T git@gitlab.com

#测试gitbub
ssh -T git@github.com

#测试gitee
ssh -T git@gitee.com

以gitee为例:

出现如上图信息说明配置成功啦!

接下来就可以愉快的使用不同的账户管理对应的远程仓库啦!!!

虚心求教
原文地址:https://www.cnblogs.com/yangchin9/p/15582575.html