配置SSH Key到GitHub并提交代码

在工作中,我们需要通过git来拉取和提交代码到github上,这就需要我们在本地配置ssh。
关于SSH的介绍不用多说了,相信大家应该都清楚,下面直接进入主题:

1.生成SSH Key

在Linux和Mac系统中都自动安装了SSH,Windows系统需要安装Git Bash。
首先检查下本机是否已经安装了SSH,在终端输入ssh即可:
这里写图片描述
接下来就是生成ssh key了,输入ssh-keygen -t rsa,然后连续按回车键三次(注意:千万不要输入密码!)。
这里写图片描述
出现上面内容就说明成功生成id_rsaid_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就可以了!
关键点:

  1. Username for 'https://github.com': 输入的还是自己github帐号名
  2. Password for 'https://lemonwang0110@github.com':输入的就是token令牌号

其它参考:
https://blog.csdn.net/Forbest1/article/details/127937452
https://blog.csdn.net/u014090429/article/details/126509415

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值