如果以前设置过用户名或者邮箱先清除信息。
一、查看是否设置过用户名,邮箱:git config --list
查看是否有 user.email,user.name
二、如果有清除设置
git config --global --unset user.name
git config --global --unset user.email
如果没有忽略这一步
清除完之后执行一确认。
三、生成 ssh key
MacBook-Pro:.ssh$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/用户名/.ssh/id_rsa):
此步可重命名秘钥文件名及路径,如果直接回车,默认为 id_rsa. 路径为/Users/用户名/.ssh/id_rsa
然后一路回车,最终如下:
MacBook-Pro:.ssh$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NfAJbzLwTRwImb4atVepSbtAbg1qH0Lm0B9/t4aCw2g [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| o+ooo. |
| oo.B.. |
| . . + O. |
| . + * .=o. |
| = * OS= |
| * O B . . |
| . O = o o . |
| E = o . o |
| . . . . |
+----[SHA256]-----+
执行:cd ~/.ssh命令查看该文件下秘钥文件
同样方式执行生成多个账户的 ssh key;
注意:如果多个 ssh key 需要重命名秘钥文件名;
四、打开秘钥.pub文件,将全部文件内容添加到 git 中 ssh key 中即可。
五、将秘钥添加到高速缓存中
先查看是否已经添加:
ssh-add -l
然后执行命令添加秘钥:
ssh-add ~/.ssh/私钥文件名
六、添加配置文件设置
查看.ssh文件夹下是否存在 config 文件,如果不存在,创建 touch config
添加内容:
#github
host github //别名
Hostname github.com //连接域名
User xxx //账户
IdentityFile ~/.ssh/id_rsa_git //对应的私钥文件
#giblab [email protected]
host gitlab
Hostnmae gitlab.xxx.cn
User xxx1
IdentityFile ~/.ssh/id_rsa_xxx
到此,所有配置工作全部完成。
七、git连接测试:
ssh -T [email protected]
出现如下:
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
注意:我用同样的方式测试连接 gitlab 时,则出现了问题。
MacBook-Pro:.ssh$ ssh -T gitlab.xxx.cn
The authenticity of host 'gitlab.xxx.cn (xxx)' can't be established.
RSA key fingerprint is SHA256:lOjIwuQpu51x9mo86jmZrRw8fp/RAQKvB92VUKkuesM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.xxx.cn,xxx' (RSA) to the list of known hosts.
[email protected]'s password:
Connection closed by xxx port 22
目前不知道什么原因,最后尝试克隆项目,成功。也算是连接成功。
补充:
最后一步,确认了问题,命令用错了
应该是:ssh -T [email protected] 这样就没问题了
回车出现:Welcome to GitLab, xxx!
完成