如何将 GitHub 上的主分支从 master 迁移到 main?

5
我在 git-hub 上有一个 master 分支和一个 main 分支。我意识到我的一切都被推到了 master 分支,这不是有意的。然而,git-hub 现在使用 main 而不是 master 作为第一顺位。我该如何将我的 master 分支移动到 main 分支中?
5个回答

4

有多种方法可以实现这一点,但使用纯Git可以按如下方式完成:

#Change to the main branch
git checkout main
#Rebase the main branch on top of the current master. In easy words, go to the last point the two branches have in common, then add all the changes master did in the meanwhile and then add the changes done to main. If main is empty this is equivalent to 
# git checkout master; git branch -D main; git checkout -b main
#which deletes the current main and then copies master over to main
git rebase master
# push back to GitHub (--force, because rabse does not add a commit, but changes the status, so push will be rejected without it)
git push --force

2
您能解释一下这个代码的作用以及为什么它可以解决问题吗? - camille

4

您可以通过访问Github网站界面来删除或重命名分支:

https://github.com/YourName/YourRepo/branches

目前可以通过仓库首页的branches链接访问此页面: https://i.imgur.com/64Zc2n6.png

您可以单击书写工具按钮来重命名相应的分支: https://i.imgur.com/tn7BqPS.png

值得注意的是,通过 Github 删除和重命名分支与使用 CLI git 或 GUI git 客户端是一样具有潜在破坏性的。当然,您可以从本地克隆重新推送分支,但如果您感到不确定,应确保该克隆已经更新后再进行操作。


0

将本地主分支设置为跟踪远程主分支:

git branch --set-upstream-to origin/main master

第一次拉取时会显示错误 refusing to merge unrelated histories,因此您可以使用以下命令拉取:git pull origin main --allow-unrelated-histories


-1
在GitHub Web界面中从master创建一个PR到main。 如果您想在本地执行此操作:
git checkout main
git merge master git push

-1

将其推送到 GitHub 的主分支。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接