在工作中,我们需要通过git来拉取和提交代码到github上,这就需要我们在本地配置ssh。
关于SSH的介绍不用多说了,相信大家应该都清楚,下面直接进入主题:
1.生成SSH Key
在Linux和Mac系统中都自动安装了SSH,Windows系统需要安装Git Bash。
首先检查下本机是否已经安装了SSH,在终端输入ssh即可:
接下来就是生成ssh key
了,输入ssh-keygen -t rsa
,然后连续按回车键三次(注意:千万不要输入密码!)。
出现上面内容就说明成功生成id_rsa
和id_rsa.pub
两个文件,id_rsa.pub
为公钥,id_rsa
为私钥,它们都是隐藏文件。这里说明下生成的公钥和私钥所在位置,Linux和Mac系统在 ~/.ssh
下面,Windows系统在C盘Documents and Settings/username/.ssh
下面。
那么如何查看它们的内容呢?只需要继续执行以下两条命令即可。
# .ssh就在系统根目录下
cd ~/.ssh
# 查看一下
ls
可以看到,执行完ls命令后,可以看到公钥和私钥。继续执行以下命令(此处为Linux和Mac系统下获取公钥内容,下面会用到公钥)即可得到公钥的内容:
cat id_rsa.pub
Windows系统查看公钥可以使用Sublime或者其他编辑器。
2.添加SSH Key到GitHub上
这里需要将公钥id_rsa.pub
添加到GitHub上,登陆GitHub进入设置界面,如图所示:
接着执行下面操作:
点击New SSH Key按钮后进行Key的填写操作,完成SSH Key的添加。如下图:
添加SSH Key成功之后,继续输入命令进行测试。
ssh -T git@github.com
注意“Are you sure you want to contine onnecting(yes/no)?”
部分,一定要输入:yes
。
出现上图结果则说明添加SSH Key成功。
3.问题-更新于2023-09-20
在拉取、提交代码到github仓库、或执行指令ssh -T git@github.com
时,可能会出现以下警告:
Warning: the ECDSA host key for 'github.com' differs from the key for the IP address '20.205.243.166
Offending key for IP in /Users/admin/.ssh/known_hosts:11
Matching host key in /Users/admin/.ssh/known_hosts:32
Are you sure you want to continue connecting (yes/no)?
原因:
- ssh 过期了
- 没设置 ssh, 或者设置没生效
解决办法:
# 新建一个 ssh
mkdir -p ~/.ssh
# 生成 ssh
ssh-keyscan -t rsa domain.com >> ~/.ssh/known_hosts
ssh-keygen -t rsa -C "你仓库绑定的邮箱"
# 回车直到生成成功
# 查看公钥并复制到 github 的 SSH Key
cat ~/.ssh/id_rsa.pub
之后再执行指令ssh -T git@github.com
测试
wwq@JNAI-T30:~$ ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Hi LemonWang0110! You've successfully authenticated, but GitHub does not provide shell access.
这样就可以了。
4. 提交代码-更新于2023-09-20
比如在github上已经创建好了一个空的仓库。
在本地,代码文件放在了workspace路径下,我想将他们上传到github创建好的空仓库中,如何做呢?
第一步:将workspace初始化为一个仓库
cd workspace
# 执行 git init 初始化git仓库之后会默认生成一个主分支 master
git init
第二步:添加所有文件
git add .
第三步:提交所有文件
# 提交信息随意写
git commit -m "first commit"
第四步:将本地仓库与github仓库做关联
# origin 是给这个项目的远程仓库起的名字,名字你可以随便取,只不过大家公认的只有一个远程仓库时名字就是 origin
# https://github.com/LemonWang0110/YOLOv8_PTQ.git是github上创建的仓库地址
git remote add origin https://github.com/LemonWang0110/YOLOv8_PTQ.git
第五步:提交代码
# 就是默认向 GitHub 上的 YOLOv8_PTQ 目录提交了代码,而这个代码是在 master 分支。当然你可以提交到指定的分支
# 提交时可能需要登录github帐号名和密码
git push origin master
注意:如果出现输入帐号名和密码后报错
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
这其实是2021年 8 月 13日,github中通过 用户名然后输入密码的认证方式被移除了,只能通过个人访问码的方式进行认证。
如何解决?
直接参考博客:Git上传文件代码到GitHub就可以了!
关键点:
Username for 'https://github.com':
输入的还是自己github帐号名Password for 'https://lemonwang0110@github.com':
输入的就是token令牌号
其它参考:
https://blog.youkuaiyun.com/Forbest1/article/details/127937452
https://blog.youkuaiyun.com/u014090429/article/details/126509415