当前位置: 首页 > 工具软件 > Fatal > 使用案例 >

git push出现 “fatal: 发送请求时出错。”的一种特殊情况

艾仲渊
2023-12-01

问题描述

前提

ssh -T git@github.com 命令连接正常;

git clone、git pull 正常,github页面 `Settings/SSH and GPG keys` 确认已添加过本地ssh公钥。

注:git clone 出现类似问题的,可以考虑先移步git clone出现 fatal: unable to access 'https://github.com/...'的解决办法(亲测有效)_明天也要加油鸭的博客-CSDN博客

$ ssh -T git@github.com
Hi birdflyi! You've successfully authenticated, but GitHub does not provide shell access.

问题

git push 失败,提示fatal: 发送请求时出错,并报告不再支持密码认证方式,需要使用personal access token替代密码认证方式(然而前面已确认github页面添加过本地的ssh公钥)。

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git push --set-upstream origin main
fatal: 发送请求时出错。
fatal: 请求被中止: 未能创建 SSL/TLS 安全通道。
fatal: 发送请求时出错。
fatal: 请求被中止: 未能创建 SSL/TLS 安全通道。
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
fatal: Authentication failed for 'https://github.com/birdflyi/Research-Methods-of-Cross-Science/'

remote报错:2021年8月13日,移除了对密码验证方式的支持。请改用个人访问令牌。

官方的解释:https://github.blog/changelog/2021-08-12-git-password-authentication-is-shutting-down/

已有相关博客解释说明此变化,并给出了配置github上的ssh Keys的方法[1]:GitHub不再支持密码验证解决方案:SSH免密与Token登录配置 - 爱码网

此博客还包含使用令牌的好处说明:GitHub不再支持密码验证解决方案:SSH免密与Token登录配置 - 爱码网

问题特征

特殊情况的特征在于:git remote -v 显示配置的url是http或https协议的,而配置的ssh需要使用git@github.com形式的ssh协议url。

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git remote -v
origin  https://github.com/birdflyi/Research-Methods-of-Cross-Science (fetch)
origin  https://github.com/birdflyi/Research-Methods-of-Cross-Science (push)

解决方案

综上,github的ssh Keys配置(详见[1]不再赘述)没有问题的情况下,将remote的url改成ssh所需的git@github.com形式即可。

即:

$ git remote remove origin 

$ git remote add origin git@github.com:birdflyi/Research-Methods-of-Cross-Science

实践中会有一些额外的操作才能使用默认的 git push 命令:

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git remote -v
origin  https://github.com/birdflyi/Research-Methods-of-Cross-Science (fetch)
origin  https://github.com/birdflyi/Research-Methods-of-Cross-Science (push)

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git remote remove origin

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git remote add origin git@github.com:birdflyi/Research-Methods-of-Cross-Science

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git remote -v
origin  git@github.com:birdflyi/Research-Methods-of-Cross-Science (fetch)
origin  git@github.com:birdflyi/Research-Methods-of-Cross-Science (push)

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main


Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git push --set-upstream origin HEAD:main
Everything up-to-date
branch 'main' set up to track 'origin/main'.

Adan@Adan-PC MINGW64 /e/gitRepo/Research-Methods-of-Cross-Science (main)
$ git push
Everything up-to-date

 类似资料: