1 清除 git 的全局设置(针对已安装 git)
新安装 git 跳过。
若之前对 git 设置过全局的 user.name 和 user.email。类似(用git config --global --list 进行查看你是否设置)
$ git config --global user.name "你的名字"
$ git config --global user.email "你的邮箱"
必须删除该设置
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"
2 生成新的 SSH keys
(1)#GitHub的钥匙
$ ssh-keygen -t rsa -C '你的邮箱' -f ~/.ssh/github_id_rsa
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/github_id_rsa
(2)#gitlab
$ ssh-keygen -t rsa -C '你的邮箱' -f ~/.ssh/gitlab_id_rsa
Enter file in which to save the key (/Users/kingboy/.ssh/id_rsa): /Users/kingboy/.ssh/gitlab_id_rsa
注意:输入的是钥匙的位置和名称。github和gitlab是不同的。
(3)完成后会在~/.ssh/目录下生成以下文件:
github_id_rsa
github_id_rsa.pub
gitlab_id_rsa
gitlab_id_rsa.pub
3 添加识别 SSH keys 新的私钥(此步如果出错,直接跳过)
默认只读取 id_rsa,为了让 SSH 识别新的私钥,需要将新的私钥加入到 SSH agent 中
$ ssh-agent bash
$ ssh-add ~/.ssh/github_id_rsa
$ ssh-add ~/.ssh/gitlab_id_rsa
4 多账号必须配置 config 文件
若无 config 文件,则需创建 config 文件
$ touch ~/.ssh/config # 创建config文件(可使用 write config 命令进行对config文件的编辑)
# gitlab
Host 你的gitlab地址
HostName 你的gitlab地址
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# 配置文件参数
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
# HostName : 要登录主机的主机名
# User : 登录名
# IdentityFile : 指明上面User对应的identityFile路径
5 在github和gitlab网站添加ssh
6 测试是否连接成功
# 测试github
$ ssh -T git@github.com
# 测试gitlab
$ ssh -T git@你的gitlab地址
---------------------
原文作者:长是人千离
原文:https://blog.csdn.net/qq_30227429/article/details/80229167