【问题标题】:My Github repo has 'main' and 'master' branches - what is their purpose?我的 Github 仓库有“主”和“主”分支——它们的目的是什么?
【发布时间】:2021-03-09 06:01:48
【问题描述】:

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

我听从了here的建议

cd <local_dir>
git init
git add .
git commit -m "initial commit"

然后我在 github 上创建了一个新的 repo 并做了

git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease

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

两个都需要吗?

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

目前这只是让我感到困惑

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

git branch -m master main
git push origin HEAD

但我遇到了一个错误

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.

我错过了什么?

【问题讨论】:

  • 是的,你可以。 Git 使用master 作为默认分支的名称。 Github 最近决定改一下main。您可以选择其中一个。

标签: git github version-control


【解决方案1】:

在美国发生 2020 年事件后,GitHub 决定将默认 Git 分支重命名为 main (details)。 Git 和 GitHub 不强制执行任何分支名称,但存储库需要默认分支;这个分支如何命名由你决定。

至于您的第二个问题,选择main 很好。在进行任何更改之前,请检查您的提交是在哪个分支上:

  • 如果您在master 上进行了提交,则必须将master 合并到main,然后删除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. 这是典型的解决方案。

【讨论】:

  • 值得注意的是,如果您选择master,您需要进入存储库设置并将其设置为“默认分支”,然后才能删除main。我相信如果您在它被标记为默认值时尝试删除它,它会抱怨。
  • 我在原始问题的末尾添加了更新。如果可能的话,会感谢更多的 cmets 吗?
【解决方案2】:

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

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

从技术角度来看,您可以保留两者,选择一个,或者甚至将两者都删除,然后决定您的默认分支称为海绵宝宝。

【讨论】:

  • 作为一个相对的新手,现在最好坚持一个名字。 “主要”听起来不错。那么我是否在本地进行更改并将更改推送到 github?
  • @FeargalHogan 是的,差不多。您可以在master 之上重新设置main 以确保您拥有所需的所有提交,并且它们可以在本地和远程删除`master(有关更多详细信息,请参阅stackoverflow.com/q/2003505/2422776
  • GitHub 已经做出了改变; Git 本身的主要作者没有。有一个技术原因:首先需要在测试套件中修复一堆东西。我尽量避免自己触及这个政治。 :-)
猜你喜欢
  • 2021-04-09
  • 2017-08-17
  • 2021-01-22
  • 1970-01-01
  • 2021-05-16
  • 2017-02-14
  • 2020-01-05
  • 2017-04-03
  • 2019-06-13
相关资源
最近更新 更多