git - 推送到 main 被拒绝,Git 只推送到 master
问题描述
我对main
和master
分支之间的区别感到非常困惑。现在有了 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
. 我没有看到在main
代码打开时打开自述文件的额外好处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.
解决方案
分支名称有两种概念,如果您进行了一些手动重命名(例如,为了从主命名移动到主命名),您可能会遇到这种情况。
最简单的解决方法是编辑您的.git/config
文件并将您找到的“master”的任何用法更改为“main.
另一种方法实际上是删除远程存储库重新创建它git remote add
并再次推送到它。
推荐阅读
- blazor-webassembly - 有没有办法从与 SetBrowserRequestCache 兼容的 NSWAG 生成客户端?
- c - 为什么 PC-Lint 抱怨重新声明(错误 18)?
- intellij-idea - IntelliJ 将单元测试链接到生成的源代码(.class 文件)而不是实际源代码(.java 文件)
- angular - 将 tslint 迁移到 @angular-eslint/schematics:ERESOLVE 无法解析依赖树:@angular/compiler@12.1.2
- python - pycaret 和 H2O 的异常检测结果不同
- php - AS400 上的 Laravel 8 使用带点的 ODBC 表名
- javascript - 在使用 JSON 值之前检查是否存在的更简单的方法
- ecmascript-6 - 使用 object.defineProperty 或 reflet.defineProperty 后对象属性未定义
- docker-compose - 通过环境变量 docker-compose redis 密码
- asp.net-core - .NET Core 的强制登录 OIDC 中间件