How to migrate a git project from using master as the default branch to main?

Viewed 110

I'm the maintainer of a project that's hosted on github/gitlab and 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.

2 Answers

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.

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.

Related