无法推送代码至GitHub仓库(403错误)及分支匹配问题求助
Git推送到GitHub的问题分析与解决方法
操作与报错整理
$ git remote add origin https://github.com/shaurya2412/practice.git saxen@LAPTOP-E6U7B2OT MINGW64 ~/OneDrive/Desktop/New folder (2) (master) $ git push -u origin main error: src refspec main does not match any error: failed to push some refs to 'https://github.com/shaurya2412/practice.git' saxen@LAPTOP-E6U7B2OT MINGW64 ~/OneDrive/Desktop/New folder (2) (master) $ git push -u origin master remote: Permission to shaurya2412/practice.git denied to ShaURYA123CODER. fatal: unable to access 'https://github.com/shaurya2412/practice.git/': The requested URL returned error: 403 saxen@LAPTOP-E6U7B2OT MINGW64 ~/OneDrive/Desktop/New folder (2) (master) $ git config --global push.autoSetupRemote true saxen@LAPTOP-E6U7B2OT MINGW64 ~/OneDrive/Desktop/New folder (2) (main) $ git remote set-url origin https://shaurya2412@github.com/user/practice.git
问题原因与解决方法
1. error: src refspec main does not match any
- 原因:本地当前分支为
master,但尝试推送不存在的main分支,本地无main分支。 - 解决方法:
- 直接推送本地
master分支:git push -u origin master(需先解决权限问题) - 或重命名本地分支为
main后推送:git branch -m master main git push -u origin main
- 直接推送本地
2. remote: Permission to shaurya2412/practice.git denied to ShaURYA123CODER(403错误)
- 原因:当前Git使用的账号是
ShaURYA123CODER,该账号无shaurya2412/practice.git仓库的推送权限;且后续修改远程URL时错误将仓库路径写成user/practice.git,应为shaurya2412/practice.git。 - 解决方法:
- 修正远程仓库URL:
git remote set-url origin https://github.com/shaurya2412/practice.git - 确认并切换Git登录账号:
- 查看当前账号:
git config --global user.name git config --global user.email - 若不是
shaurya2412的账号,修改为正确信息:git config --global user.name "shaurya2412" git config --global user.email "你的GitHub绑定邮箱"
- 查看当前账号:
- 处理权限验证:
- HTTPS方式:推送时输入
shaurya2412的GitHub账号密码,若开启2FA,需使用个人访问令牌(PAT)替代密码。 - SSH方式:生成SSH密钥并添加到GitHub账号,修改远程URL为SSH格式:
git remote set-url origin git@github.com:shaurya2412/practice.git
- HTTPS方式:推送时输入
- 协作者权限申请:若你确实使用
ShaURYA123CODER账号,联系仓库所有者shaurya2412将你添加为仓库协作者,获取推送权限。
- 修正远程仓库URL:
内容的提问来源于stack exchange,提问作者Shaurya Saxena
相关产品推荐
相关产品推荐

