如何将GIT项目从使用Master作为MAIN的默认分支迁移?

发布于 2025-02-02 15:13:08 字数 178 浏览 4 评论 0原文

我是在Github/Gitlab上托管的项目的维护者,该项目有许多贡献者。我想从使用Master作为默认分支到main。我该怎么做而不弄乱?

我已经阅读了有关该主题的一些博客 - 但希望在Stackoverflow上进行讨论,其中评论可能会指出一个人可能会忽略的问题。

I'm the maintainer of a project that's hosted on github/gitlab which has a number of contributors. I want to migrate from using master as the default branch to main. How do I do that without messing up?

I've read some blog-posts on the topic, but would prefer a discussion over here at StackOverflow where comments might point out issues that a single person could have overlooked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2025-02-09 15:13:08

Github提供了一个迁移选项,因此很琐碎。但是,即使有任何其他服务(例如Gitlab)还没有类似的东西,只要您执行正确的步骤,至少从技术的角度来看,它相对简单。

无论哪种方式,除非您的项目很小,否则最大的麻烦肯定会让您的所有贡献者正确地将修复程序正确地应用于进行中。

您需要做的

github

在项目页面上,导航到设置,在“默认分支”部分中,按编辑按钮,然后从Master更改它 Main

gitlab,又名手动迁移

我假设您的远程/上游称为onecount - 如果称为“不同的事物”在所有命令中。

  1. 将最新的Master重命名为main,然后将其推到上游:
git fetch origin
git branch --move master main
git push --set-upstream origin main
  1. 告诉您的上游,main是新默认值:

    • 导航到设置/存储库,“默认分支”,选择main,然后按“保存更改”。然后,在同一页面上,转到“受保护分支”部分,然后保护main和Unprotect Master,然后按“保存更改”。
  2. 删除Master上游 - 并不是严格必要的,但确实减少了混乱:

git push origin --delete master
  1. 迁移打开的合并/拉动请求,希望将要将其推入master要将其推入main main < /代码>而不是:
    • 在打开的合并请求中,按“编辑”。在顶部,它应该说“来自&lt; branch_name&gt; to master”。将下拉纸从Master更改为main

据我所知,您的合作者/同事需要做的事情

git fetch origin --prune && git branch -m master main && git branch --set-upstream-to=origin/main main

- 就是这样 - &amp;&amp; s是一个好主意,以防万一某些东西加起来。


如果您在此建议下遇到问题,请发表评论。 git fuckups对解开而言,所以我们至少可以做的就是尝试并不重塑轮子比我们需要的要多。

Github offers a migration option, so it's trivial. But even with any other service like gitlab that doesn't have something like that (yet), as long as you're doing the right steps it's relatively straight-forward, at least from a technical point of view.

Either way, unless your project is quite small, the biggest hassle will surely be getting all your contributors to apply fixes to work-in-progress correctly.

What you need to do

Github

On your project page, navigate to settings, and in the section "Default branch", press the edit button and change it from master to main.

Gitlab, aka migrating manually

I'm assuming that your remote/upstream is called origin - if it's called something different, replace origin with that name in all commands accordingly.

  1. Rename the most recent master to main, and push it upstream:
git fetch origin
git branch --move master main
git push --set-upstream origin main
  1. Tell your upstream that main is the new default:

    • Navigate to settings/repository, section "Default branch", select main and press "Save Changes". Then, on the same page, go the the section "Protected branches", and protect main and unprotect master, and press "Save Changes".
  2. Delete master on upstream - not strictly necessary, but does reduce confusion:

git push origin --delete master
  1. Migrate open merge/pull requests that wanted to be pushed into master to be pushed into main instead:
    • On an open merge request, press "edit". On the top, it should say "From <branch_name> into master". Change the drop-down from master to main.

What your collaborators/colleagues need to do

git fetch origin --prune && git branch -m master main && git branch --set-upstream-to=origin/main main

As far as I know, that's it - the &&s are a good idea to protect you in case something doesn't add up.


Please leave a comment if you ran into issues following this advice. git fuckups are hellish to untangle, so the least we can do is to try and not reinvent the wheel any more than we need to.

丶视觉 2025-02-09 15:13:08

我正在管理一个自托管的gitlab实例(v13.xy),而您不必手动进行操作 - 与Arne for Github提到的那样,它也有类似的解决方案。

首先,将您的主分支复制到主分支。
然后,转到Project 设置,单击存储库,然后更改默认分支。


之后,您可以使用上面的片段更新本地存储库(如果需要的话,请更改分支名称),或者如果您担心自己可能会弄乱某些东西,则可以删除项目并再次克隆。

I'm managing a self-hosted gitlab instance (v13.x.y), and you don't have to do it manually - there's a similar solution to this as the one mentioned by Arne for github.

First, copy your master branch to main branch.
Then, go to project Settings, click Repository, and change the default branch.

enter image description here
enter image description here

After that, you can use the above snippet to update your local repository (change the branch names if you need to), or if you're worried that you might mess something up, you can delete the project and clone it again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文