我的 Github 存储库有“主”和“主”分支 - 它们的用途是什么?

Fea*_*gan 8 git version-control github

出于可访问性的原因,我试图将一些本地未版本控制的代码推送到存储库中并将其发布到 GitHub 上。

我遵循了这里的建议

cd <local_dir>
git init
git add .
git commit -m "initial commit"
Run Code Online (Sandbox Code Playgroud)

然后我在 github 上创建了一个新的存储库并执行了

git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease
Run Code Online (Sandbox Code Playgroud)

但我现在在 github 上看到我有 2 个分支“main”和“master”,我猜 github 在创建存储库时创建了“main”,而当我从本地存储库同步时创建了“master”

我两者都需要吗?

我可以将 master 合并到 main 然后删除 master 吗?

目前这让我很困惑

注意:我现在已经尝试过(本地)

git branch -m master main
git push origin HEAD
Run Code Online (Sandbox Code Playgroud)

但我收到错误

To https://github.com/<the-repo>
 ! [rejected]          HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/<the-repo>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Htt*_*tqm 9

在 2020 年美国发生的事件之后,GitHub 决定重命名默认的 Git 分支main详情)。Git 和 GitHub 不强制要求任何分支名称,但存储库需要默认分支;该分支的命名方式取决于您。

至于你的第二个问题,解决问题main就可以了。在进行任何更改之前,请检查您的提交位于哪个分支:

  • 如果您在 上提交master,则必须合并mastermain,然后删除master
  • 如果您在 上进行了提交main,则 上没有任何内容master,并且可以将其删除。

你可以 :

  • 在您的工作站上执行此操作并push更改为 GitHub(我的首选)
  • 在 GitHub 上工作并pull从那里开始

编辑回答额外的问题:Git 很棒,因为它有相当丰富的错误消息和建议:

  • Updates were rejected because the tip of your current branch is behind its remote counterpart. 这意味着远程分支(在 GitHub 上)已更改您的本地分支未更改的内容,这就是 Git 拒绝push.
  • Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. 这是典型的解决方案。


Mur*_*nik 1

Git 不需要任何特定的分支。

旧版本的 Git(以及 GitHub)默认用于创建“主”分支。在后续版本中(IIRC,2020 年 10 月左右),默认分支被重命名为“main”,以便使用更具包容性的语言。

从技术角度来看,您可以保留两者,选择一个,甚至放弃它们并决定将默认分支称为 SpongeBob。