Update GitLab Repository Default Branch from Master to Main
- Create a new branch called
main
- Set
main
as the default branch - Add protections to the
main
branch - Update any GitLab CI to use
main
- Unprotect the
master
branch - Delete the
master
branch
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
main
as 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
master
branch because they will need to bo recreated against themain
branch. - 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
master
branch. You can see themaster
branch settings below the form. - Allowed to push = No one, or whatever was set for the
master
branch. 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.yml
file and update all occurrences ofmaster
withmain
.- 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
main
branch.
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
master
row.
Delete the master
branch
- In your GitLab repository select Repository » Branches
- Click the red trashcan icon on the
master
branch row.