首页 > 解决方案 > 推送到 main 被拒绝,Git 只推送到 master

问题描述

我对mainmaster分支之间的区别感到非常困惑。现在有了 GitHub 的新main分支,我想把所有东西都推送到那里,但我发现命令:

$ git push -u origin main

返回错误消息:

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/my_account/url.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Git 只允许我推送,master但我不明白这样做的理由是什么,因为:

有人可以指出我正确的方向吗?

**编辑:**根据下面 Wes Hardaker 的回答,我删除了 repo 和 .git,并再次执行了整个过程。请注意,当我这样做时git init,Git 出人意料地从 main 跳到了 master,所以我不得不在之后切换回 main。

$ cd "path/to/local/project/folder"
$ git init
$ git checkout -b main
$ git add .
$ git commit -m "commit msg 06/30/2021"
$ git remote add origin https://github.com/my_account/my_repo_name.git
$ git push origin main

最后一行返回以下错误:

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/leweex95/Housing-Prices-Prediction.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

标签: gitgithub

解决方案


分支名称有两种概念,如果您进行了一些手动重命名(例如,为了从主命名移动到主命名),您可能会遇到这种情况。

最简单的解决方法是编辑您的.git/config文件并将您找到的“master”的任何用法更改为“main.

另一种方法实际上是删除远程存储库重新创建它git remote add并再次推送到它。


推荐阅读