文章目录
步骤
一、在本地设置git的user name 和 email
二、检查本地电脑是否存在SSH Key
三、获取SSH Key
四、在github上添加SSH Key
五、验证和修改
一、在本地设置git的user name 和 email
如果你是第一次使用,或者还没有配置过的话需要操作一下命令,自行替换相应字段。
git config --global user.name "user name"
git config --global user.email "user email"
![](https://img.haomeiwen.com/i12879821/dd225efe920c265c.png)
![](https://img.haomeiwen.com/i12879821/57829d82258a7b86.png)
ps:git config --list 查看当前Git环境所有配置,还可以配置一些命令别名之类的。通过该命令,查看是否已经配置成功,如能看到下面的user.name 和 user.email,证明已经配置成功了。
git config --list
![](https://img.haomeiwen.com/i12879821/e26974c3dd368d45.png)
二、检查本地电脑是否存在SSH Key
cd ~/.ssh
ls
或者
ll
//看是否存在 id_rsa 和 id_rsa.pub文件,如果存在,说明已经有SSH Key
![](https://img.haomeiwen.com/i12879821/955faa2700405709.png)
如果没有SSH Key,则需要先生成一下
ssh-keygen -t rsa -C "user email"
一直按回车键就好了,最后再ls查看是否生成了 id_rsa 和 id_rsa.pub文件
![](https://img.haomeiwen.com/i12879821/4f6eb2a472f6166f.png)
执行之后继续执行上面的命令来获取SSH Key
cd ~/.ssh
ls
或者
ll
//看是否存在 id_rsa 和 id_rsa.pub文件,如果存在,说明已经有SSH Key
三、获取SSH Key
cat id_rsa.pub
//拷贝秘钥 ssh-rsa开头
![](https://img.haomeiwen.com/i12879821/31862c05f0681c1e.png)
四、在github上添加SSH Key
GitHub点击用户头像,选择Settings
![](https://img.haomeiwen.com/i12879821/4eb79fb4da3bd81c.png)
新建一个SSH Key
![](https://img.haomeiwen.com/i12879821/c2ef2b4f7ffad18c.png)
取个名字,把之前拷贝的秘钥复制进去,添加就好啦。
![](https://img.haomeiwen.com/i12879821/7f42aee8195d1801.png)
![](https://img.haomeiwen.com/i12879821/31862c05f0681c1e.png)
五、验证和修改
测试是否成功配置SSH Key
ssh -T git@github.com
//运行结果出现类似如下
Hi xiangshuo1992! You've successfully authenticated, but GitHub does not provide shell access.
![](https://img.haomeiwen.com/i12879821/f3cf7d52f509e731.png)
之前已经是https的链接,现在想要用SSH提交怎么办?
直接修改项目目录下 .git文件夹下的config文件,将地址修改一下就好了。
![](https://img.haomeiwen.com/i12879821/8f40c18028542ae6.png)
![](https://img.haomeiwen.com/i12879821/a3dd29fca2ca4a16.png)
git地址获取可以看下图如何切换。
![](https://img.haomeiwen.com/i12879821/480ecc2fed59cd5c.png)
![](https://img.haomeiwen.com/i12879821/f7683f0c3d2ee85e.png)
网友评论