1. 查看当前git是否存在全局用户名邮箱
git config user.name
git config user.email
2. 如果已经设置全局用户名邮箱请先清除
git config --global --unset user.name "你的名字"
git config --global --unset user.email "你的邮箱"
3. 生成密钥
A. 生成gitlab密钥一般是用你在公司用的邮箱,就是gitlab绑定的邮箱:
ssh-keygen -t rsa -C "办公邮箱"
点击回车会提示如下,你需要填写红色部分我用颜色标记
Enter file in which to save the key (/c/Users/honey/.ssh/id_rsa): id_rsa_gitlab
B. 生成github密钥一般是用自己的邮箱,我常用qq邮箱邮箱:
ssh-keygen -t rsa -C "qq邮箱"
点击回车会提示如下,你需要填写红色部分我用颜色标记
Enter file in which to save the key (/c/Users/honey/.ssh/id_rsa): id_rsa_gitlhub
C. 生成gitee密钥一般是用自己的邮箱,我常用qq邮箱邮箱:
ssh-keygen -t rsa -C "qq邮箱"
点击回车会提示如下,你需要填写红色部分我用颜色标记
Enter file in which to save the key (/c/Users/honey/.ssh/id_rsa): id_rsa_gitee
4. 文件生成
此时在此目录下会生成如下文件:
- id_rsa_github
- id_rsa_github.pub
- id_rsa_gitlab
- id_rsa_gitlab.pub
- id_rsa_gitee
- id_rsa_gitee.pub
5. 文件拷贝
将这些文件全部拷贝到Windows .shh目录下,即:
C:\Users\Windows登录名\.ssh
6. 创建config文件
需要在.ssh文件夹下新建config文件,先新建config.txt,然后修改文件名去掉后缀。
config文件内容如下:
其中git.xxx.cn
对应你使用的gitlab仓库地址
# gitlab
Host git.xxx.cn
HostName git.xxx.cn
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
User git
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
User git
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User git
7. 最后将各自的id_rsa.pub打开复制里面的公钥到对应的公钥添加里,添加前先删除原有的公钥,接下来就可以直接使用ssh拉取推送代码了