Update GitLab Repository Default Branch from Master to Main
- Create a new branch called
main - Set
mainas the default branch - Add protections to the
mainbranch - Update any GitLab CI to use
main - Unprotect the
masterbranch - Delete the
masterbranch
Create main Branch
In the Web User Interface
- Navigate to the GitLab repository you want to update.
- Click the + dropdown menu.
- Click New branch
- Enter
mainas the Branch name - Ensure Create from is set to master
- Click Create branch
In the Command Line
git checkout master- switch to the master branchgit pull- make sure the master branch is up to dategit checkout -b main- create the main branchgit push --set-upstream origin main- push the branch to GitLab
Set main as the Default Branch
- In your GitLab repository select Settings » Repository
- Expand the Default Branch section
- In the Detault Branch dropdown select
main - Ensure the Auto-close referenced issues on default branch checkbox is checked. This will close all open merge requests to the
masterbranch because they will need to bo recreated against themainbranch. - Save changes
Protect the main Branch
- In your GitLab repository select Settings » Repository
- Expand the Protected Branches section
- Fill out the Protect a branch form
- Branch =
main - Allowed to merge = Maintaners, or whatever was set for the
masterbranch. You can see themasterbranch settings below the form. - Allowed to push = No one, or whatever was set for the
masterbranch. I prefer not to let anyone to push directly to the default branch.
- Branch =
- Click Protect
Update CI
If you have a .gitlab-ci.yml file some of the continuous integration may revolve around the master branch. This will need to be updated to reference the main branch instead of master.
- Create a new branch
git checkout -b fix/main-ci - Edit your
.gitlab-ci.ymlfile and update all occurrences ofmasterwithmain.- If you are using vim you could accomplish this with
:%s/master/main/g - Otherwise use the find replace functionality of your editor.
- If you are using vim you could accomplish this with
- Add, commit and push the changes.
git add *git commit -m "updated ci to use main instead of master"git push --set-upstream origin fix/main-ci
- Merge the changes
into the
mainbranch.
Unprotect the master Branch
- In your GitLab repository select Settings » Repository
- Expand the Protected Branches section
- In the protected branch listing at the bottom of the section click unprotect on the
masterrow.
Delete the master branch
- In your GitLab repository select Repository » Branches
- Click the red trashcan icon on the
masterbranch row.