我的 Github 仓库有“主"和“主"分支——它们的目的是什么? [英] My Github repo has 'main' and 'master' branches - what is their purpose?

查看:48
本文介绍了我的 Github 仓库有“主"和“主"分支——它们的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to push some local unversioned code into a repo and get it up onto GitHub for accessibility reasons.

我遵循了建议 这里

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

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

Then I created a new repo on github and did

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"

But I now see on github that I have 2 branches 'main' and 'master' I guess github created 'main' when it created the repo and I created 'master' when I synced from my local repo

两个都需要吗?

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

Can I just merge master into main and then delete master?

目前只是让我困惑

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

Note: I have now tried (locally)

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.

我错过了什么?

推荐答案

在美国发生 2020 年事件后,GitHub 决定重命名默认 Git 分支 main (详细信息).Git 和 GitHub 不强制任何分支名称,但存储库需要一个默认分支;该分支的命名方式由您决定.

After events of 2020 in the USA, GitHub decided to rename the default Git branch main (details). Git and GitHub do not enforce any branch name, but a repository needs a default branch; how this branch is named is up to you.

至于你的第二个问题,解决 main 没问题.在进行任何更改之前,请检查您的提交是哪个分支:

As for your 2nd question, settling on main is fine. Before doing any changes, check on which branch your commits are :

  • 如果您在 master 上提交,则必须将 master 合并到 main,然后删除 master
  • 如果您在 main 上进行了提交,则 master 上没有任何内容并且可以将其删除.
  • if you made your commits on master, you'll have to merge master to main, then delete master
  • if you made your commits on main, there is nothing on master and and can delete it.

你可以:

  • 在您的工作站上执行此操作并将更改推送到 GitHub(我的首选)
  • 在 GitHub 上工作并从那里 pull
  • do this on your workstation and push changes to GitHub (my preferred choice)
  • work on GitHub and pull from there

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

EDIT to answer the extra question : Git is great in that it has fairly informative error messages with suggestions :

  • 更新被拒绝,因为您当前分支的提示落后于它的远程副本.这意味着远程分支(在 GitHub 上)有您本地分支没有的更改,这就是 Git 拒绝 push 的原因.
  • 在再次推送之前集成远程更改(例如提示:'git pull ...').这是典型的解决方案.
  • Updates were rejected because the tip of your current branch is behind its remote counterpart. This means the remote branch (on GitHub) has changes your local branch has not, which is why Git refuses to push.
  • Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. This is the typical solution.

这篇关于我的 Github 仓库有“主"和“主"分支——它们的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆