Git - 本地同时配置GitHub、Gitlab、Gitee的SSH-Key

一:生成各平台的密钥

1.配置gitlab的ssh-key:

命令行操作输入,期间会要求输password,不用输入,直接按键enter即可:

1.mac环境下
//进入.ssh路径
$ cd ~/.ssh
//生成gitlab密钥,-f id_rsa_gitlab 为指定文件名,默认为id_rsa
//             【-C "GitlabAccount"为描述信息,选填】
$ ssh-keygen -t rsa -C "你的gitlab邮箱" -f id_rsa_gitlab -C "GitlabAccount"


2.windows环境下
$ cd C:\Users\Administrator\.ssh
$ ssh-keygen -t rsa -C "你的gitlab邮箱" -f id_rsa_gitlab -C "GitlabAccount"

2.配置github的ssh-key:

命令行操作输入:

1.mac环境下
//进入.ssh路径
$ cd ~/.ssh
//生成GitHub密钥【-f id_rsa_github 为指定文件名,默认为id_rsa】
//             【-C "GithubAccount"为描述信息,选填】
$ ssh-keygen -t rsa -C "你的github邮箱" -f id_rsa_github -C "GithubAccount"


2.windows环境下
$ cd C:\Users\Administrator\.ssh
$ ssh-keygen -t rsa -C "你的github邮箱" -f id_rsa_github -C "GithubAccount"

3.配置gitee的ssh-key:

命令行操作输入:

1.mac环境下
//进入.ssh路径
$ cd ~/.ssh
//生成Gitee密钥,-f id_rsa_gitee 为指定文件名,默认为id_rsa
//             【-C "GithubAccount"为描述信息,选填】
$ ssh-keygen -t rsa -C "你的gitee邮箱" -f id_rsa_gitee -C "GiteeAccount"


2.windows环境下
$ cd C:\Users\Administrator\.ssh
$ ssh-keygen -t rsa -C "你的gitee邮箱" -f id_rsa_gitee -C "GiteeAccount"

 4.将每个密钥.pub文件打开,复制其内容,粘贴到对应github、gitlab、gitee官网里创建的ssh-key里

 二:将多个平台密钥共同写入配置

这一步才能使每个密钥在本地环境生效


//命令行执行,创建config文件
$ touch ~/.ssh/config

//可以看到.ssh文件下有个config文件,文本方式打开
//输入以下内容后保存即可:
Host github.com
HostName github.com
User 你的github邮箱                
IdentityFile ~/.ssh/id_rsa_github
 
Host gitlab.xxx.cn                //host后边为公司gitlab域名
HostName gitlab.xxx.cn            //同上为公司gitlab域名
User 你的gitlab邮箱                
IdentityFile ~/.ssh/id_rsa_gitlab

Host gitee.com
HostName gitee.com
User 你的gitee邮箱
IdentityFile ~/.ssh/id_rsa_gitee
PreferredAuthentications publickey

 三:验证

//命令行输入测试连接
$ ssh -T [email protected]
$ ssh -t [email protected]
$ ssh -t [email protected]

Git - 本地同时配置GitHub、Gitlab、Gitee的SSH-Key_第1张图片

四.本地相关配置 

//本地配置
$ git config --local user.name  '你的gitlab账号名'   
$ git config --local user.email  '公司账号邮箱' 
//全局配置
$ git config --global user.name  '你的github账号名' 
$ git config --global user.email  '你的github账号邮箱'

你可能感兴趣的:(Git,git,ssh,github)