推送到 main 被拒绝,Git 只推送到 master

时间:2021-06-30 14:04:29

标签: git github

我对 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 但我不明白这样做的理由是什么,因为:

  • 创建一个并行分支并拆分项目
  • main 是默认设置,那么我为什么要推送到 master
  • 在 GitHub 上手动创建存储库时,还会在 main 上创建 readme.md。我认为 main 上的自述文件和 master 上的代码没有额外的好处。

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

**Edit: ** 根据下面 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.

2 个答案:

答案 0 :(得分:0)

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

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

替代方法实际上是删除远程存储库,使用 git remote add 重新创建它并再次推送。

答案 1 :(得分:0)

git 在推送到远程仓库失败时告诉你的提示非常清楚:

<块引用>

提示:更新被拒绝,因为远程包含您在本地没有的工作...您可能希望首先集成远程更改 提示:(例如,'git pull ...')在再次推送之前。

您有两个基本选项:

  1. git pullgit pull --rebase 以便在本地进行远程更改。

  2. git push --force 它将覆盖(危险!!)您的远程更改。

git 本身并不关心你的分支是叫 master 还是 main。 GitHub 与 git 应用程序不同,因此仅仅因为 GitHub 现在配置为使用 main 并不会改变 git 的行为。


附注:您可以在创建存储库时将 GitHub 的配置更改为默认使用 master(而不是使用 main)。