文章目录
步骤
一、在本地设置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"


ps:git config --list 查看当前Git环境所有配置,还可以配置一些命令别名之类的。通过该命令,查看是否已经配置成功,如能看到下面的user.name 和 user.email,证明已经配置成功了。
git config --list

二、检查本地电脑是否存在SSH Key
cd ~/.ssh
ls
或者
ll
//看是否存在 id_rsa 和 id_rsa.pub文件,如果存在,说明已经有SSH Key

如果没有SSH Key,则需要先生成一下
ssh-keygen -t rsa -C "user email"
一直按回车键就好了,最后再ls查看是否生成了 id_rsa 和 id_rsa.pub文件

执行之后继续执行上面的命令来获取SSH Key
cd ~/.ssh
ls
或者
ll
//看是否存在 id_rsa 和 id_rsa.pub文件,如果存在,说明已经有SSH Key
三、获取SSH Key
cat id_rsa.pub
//拷贝秘钥 ssh-rsa开头

四、在github上添加SSH Key
GitHub点击用户头像,选择Settings

新建一个SSH Key

取个名字,把之前拷贝的秘钥复制进去,添加就好啦。


五、验证和修改
测试是否成功配置SSH Key
ssh -T git@github.com
//运行结果出现类似如下
Hi xiangshuo1992! You've successfully authenticated, but GitHub does not provide shell access.

之前已经是https的链接,现在想要用SSH提交怎么办?
直接修改项目目录下 .git文件夹下的config文件,将地址修改一下就好了。


git地址获取可以看下图如何切换。


网友评论