1.进入ssh文件夹
cd ~/.ssh
2.生成GitHub和GitLab对应的公/私密钥
ssh-keygen -t rsa -C "personal@mail.com" -f ~/.ssh/id_rsa_gitlab
ssh-keygen -t rsa -C "personal@mail.com" -f ~/.ssh/id_rsa_github
现在会生成如下四个文件
~/.ssh/id_rsa_github
~/.ssh/id_rsa_github.pub
~/.ssh/id_rsa_gitlab
~/.ssh/id_rsa_gitlab.pub
3.网页配置公钥
# Github配置公钥
# 拷贝生成的公钥到剪切板
pbcopy < ~/.ssh/id_rsa_github.pub
# Gitlab配置公钥
# 拷贝生成的公钥到剪切板
pbcopy < ~/.ssh/id_rsa_gitlab.pub
登录Gitlab/GitHub
账户,进入Settings > SSH Keys
,粘贴公钥使用即可。
4.创建ssh配置文件
# 创建文件
touch ~/.ssh/config
# 编辑文件
vi ~/.ssh/config
以下内容为模板更改配置
# Personal github account
Host github.com
HostName github.com
User github邮箱
IdentityFile ~/.ssh/id_rsa_github
# Personal gitlab account
Host gitlab.com //gitlab网址
HostName gitlab.com //gitlab网址
User gitlab邮箱
IdentityFile ~/.ssh/id_rsa_gitlab
配置好后可以使用如下命令去测试
ssh -T git@github.com
清除 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 "你的邮箱"