You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法推送代码至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登录账号:
      1. 查看当前账号:
        git config --global user.name
        git config --global user.email
        
      2. 若不是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
        
    • 协作者权限申请:若你确实使用ShaURYA123CODER账号,联系仓库所有者shaurya2412将你添加为仓库协作者,获取推送权限。

内容的提问来源于stack exchange,提问作者Shaurya Saxena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 02:55:14