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

执行git push -u origin main报错,React项目无法推送到GitHub求助

解决 error: src refspec main does not match any 问题

这个错误的核心原因是本地Git仓库没有可推送的内容或分支与远程仓库不匹配,按以下步骤操作即可解决:

  • 初始化本地仓库(若未关联Git):

    git init
    
  • 检查并统一分支名称:
    执行git branch查看本地分支,若显示为master而非main,执行重命名命令:

    git branch -M main
    
  • 添加项目文件到暂存区:

    git add .
    

    提示:若有无需提交的文件(如node_modules),先在根目录创建.gitignore文件并写入node_modules/,再执行上述命令。

  • 提交暂存内容到本地仓库:

    git commit -m "Initial commit: React project setup"
    
  • 关联远程GitHub仓库:
    替换<你的GitHub仓库地址>为实际仓库的HTTPS/SSH地址,执行:

    git remote add origin <你的GitHub仓库地址>
    

    若之前已关联过错误的远程仓库,可先执行git remote remove origin再重新关联。

  • 推送代码到远程仓库:

    git push -u origin main
    

完成以上步骤后,你的React项目代码就能成功推送到GitHub仓库。

内容的提问来源于stack exchange,提问作者Prashant kumar Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:03:21