git push -u origin main error (other name than master)

git push -u origin main error (other name than master)

git 的命名约定已更改,现在建议对 Master 分支使用其他命名方式。

我在尝试将我很棒的项目初始化为名称为 Main

的新 git 存储库时遇到了一个问题
git init
git add -A
git remote add origin https://github.com/{MYREPO}.git
git push -u origin main

最终抛出以下错误

error: src refspec main does not match any.
error: failed to push some refs to 'https://github.com/{MYREPO}.git'

git 版本 2.16.1.windows.4

解决方案 假设这是全新的 repo,这就是解决方案

git branch -m master main
git push -u origin main

说明

git init 创建一个名为 master (ref)

的本地原点
-b <branch-name
--initial-branch=<branch-name>

Use the specified name for the initial branch in the newly created repository.

If not specified, fall back to the default name: master.

这就是 git 抛出该错误的原因。

使用git show-ref查看您有哪些参考。 (ref)

如果它显示 refs/heads/master,您可以使用 git branch -m master {name}

将其重命名为您想要的任何名称