git push 失败出现error: src refspec master does not match any

765 阅读1分钟

出现背景

如果是在2021年年中的时候出现了此问题, 那么适用于我写的这种解决方法。

出现原因

按照github的官方说法,github于2020年11月13日起逐步的推行使用token认证代替原来的用户名密码认证,该项工作于2021年年中左右推行完毕。所以一些历史上我们使用用户名+密码拉取的github仓库在进行git pull \ push \ fetch等操作时,会报如下类似的错误:

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: unable to access 'https://github.com/fuqqnl/Reinvent-Wheels.git/': The requested URL returned error: 403

解决方法

要解决这个问题,首先我们要参考官方文档来设置一个 token ,参考官方文档

具体方法

  1. 删除系统记往的密码 不同的系统删除系统记的密码不同,macos参考官方帮助文档进行删除。

  2. 配置git 的全局的认证帮助器为空


git config --local credential.helper ""

这时,我们从小执行 git push origin 分支名的时候,就会提示输入用户名和密码。

  1. 配置 git config 文件

若不想每次都输入用户名密码,则可以在项目文件夹中执行git config --edit,然后将在远程仓库的位置上指定用户名与token.

[remote "origin"]
       url = https://githubusername:youtoken@github.com/yunzhiclub/repositoryname
       fetch = +refs/heads/*:refs/remotes/origin/*

例如:

[remote "origin"]
        url = https://githubusername:test_gy_token-example@github.com/fuqqnl/Reinvent-Wheels.git
        fetch = +refs/heads/*:refs/remotes/origin/*

这时,再推送就可以了。

参考文档:github启用 personal access token后,历史项目如何进行认证变更