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

Git推送报错src refspec main不匹配,如何推送到main分支?

问题描述

执行Git提交代码时遇到以下错误:

首次操作流程

D:\Work\htdocs\temp>git add *
warning: LF will be replaced by CRLF in .htaccess.
The file will have its original line endings in your working directory\

D:\Work\htdocs\temp>git commit -m "first commit"
[master 2f28669] first commit
 1 file changed, 6 insertions(+)
 create mode 100644 .htaccess

D:\Work\VikasSite\htdocs\temp>git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

后续修正操作(仍失败)

D:\Work\htdocs\temp>git push origin --delete master
To https://github.com/*******/php_social_network.git
 - [deleted]         master

D:\Work\htdocs\temp>git commit -m "test"
[master *****] test
 2 files changed, 39 insertions(+)
 create mode 100644 github
 create mode 100644 github.pub

D:\Work\htdocs\temp>git push --set-upstream origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/******/php_social_network.git'
错误原因

你本地当前的分支还是master,但尝试推送到远程的main分支——本地根本不存在main分支,所以Git找不到对应的分支进行推送,抛出src refspec main does not match any错误。

解决步骤
  1. 将本地master分支重命名为main
    git branch -m master main
    
  2. 设置上游分支并推送代码
    git push --set-upstream origin main
    
    这条命令会把本地的main分支推送到远程仓库,同时将远程main设置为本地分支的上游,之后直接用git push就能同步代码。

如果远程仓库还没有main分支,上述推送命令会自动在远程创建该分支;如果已经存在,则直接同步本地提交。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 23:05:16