【问题标题】:git: error: src refspec main does not match anygit:错误:src refspec main 不匹配任何
【发布时间】:2022-07-14 23:26:50
【问题描述】:

我试图通过 VSCode 使用 git,但因为我不知道如何在 VSCode 上使用 Git,所以一切都搞砸了。
然后我像往常一样尝试使用 CMD (Windows)。正在学习,以前没见过这种错误,不知道怎么解决。

但是当我尝试推送到主分支时,它会显示这些错误:

error: src refspec main does not match any
error: failed to push some refs to 'myPathToRepo'

我试过了:

  • 我尝试使用另一个分支,它工作正常,但我想使用主分支
  • 我什至尝试删除 git 文件夹并重新启动它

我在 StackOverflow 上看到过其他帖子,大部分都说“先提交,然后再推送,因为当您在未提交的情况下推送时会出现此错误”。

删除.git/文件夹并重新启动后,它开始显示同样的错误

>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

>git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'myPathToRepo'

这是我的第一次提交:

[master (root-commit) 061a06e] first commit

PS。我只有“主”分支。

我运行的命令:

git init
git remote add origin 'pathToMyRepo'
git add -A
git commit -m "first commit"
git push
git push -u origin main

请帮忙。

【问题讨论】:

标签: git github


【解决方案1】:

您没有名为main 的分支。您有一个名为 master 的分支。

使用正确的名称推送:

git push -u origin master

或者将您的本地分支名称更改为 main 并使用它:

git branch -m main
git push -u origin main

【讨论】:

  • 这样做后显示此错误。 >到 pathToMyRepo ! [rejected] main -> main (fetch first) error: failed to push some refs to 'pathToMyRepo' 提示:更新被拒绝,因为远程包含您所做的工作提示:本地没有。这通常是由另一个存储库推送提示引起的:to the same ref.您可能希望在再次推送之前先集成远程更改提示:(例如,'git pull ...')。提示:有关详细信息,请参阅“git push --help”中的“关于快进的说明”。
  • 嗯,你按照错误信息的提示做了吗?
  • 我试过 git pull。然后它说“当前分支没有跟踪信息。请指定你想要变基的分支。”然后我尝试了 >git pull origin main,然后它说“致命:拒绝合并不相关的历史”。然后我做了“git pull origin main --allow-unrelated-histories”,这就解决了。然后我做了'git push -u origin main,就是这样,我现在可以像平常一样使用它了。我真的需要专注于错误,而不仅仅是让它发挥作用,这样下次我就不会遇到解决它们的问题了。但是谢谢,我会选择这个作为帮助别人的答案
【解决方案2】:

确保在设置完成后启动 VSCode:

git config --global init.defaultbranch main

这样,当您初始化一个新的存储库并使用 VSCode 进行首次提交时,您创建的默认本地分支确实是 main,而不是 master

【讨论】:

    【解决方案3】:

    只需确保您有一些代码可以提交

    git 初始化 git远程添加原点'pathToMyRepo' 混帐添加。 git commit -m "第一次提交" git push -u origin main

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 2017-03-05
      • 2012-05-21
      • 2011-08-13
      • 1970-01-01
      • 2021-03-18
      • 2014-02-11
      相关资源
      最近更新 更多