change the default branch from main to new branch created called master on gitlab
- User
change the default branch from main to new branch created called master on gitlab
- Assistant
To change the default branch in a GitLab repository from
mainto a newly created branch calledmaster, follow these steps:Step 1: Create the New Branch
If you haven't already created the
masterbranch, you can do so with the following commands:-
Clone your repository (if you haven't done so already):
git clone [YOUR_REPOSITORY_URL] cd [YOUR_REPOSITORY_NAME] -
Create and switch to the new
masterbranch:git checkout -b master -
Push the new branch to GitLab:
git push origin master
Step 2: Change the Default Branch in GitLab
-
Go to Your Project: Log in to GitLab and navigate to your project.
-
Open Settings: Click on
Settingsin the left sidebar. -
Navigate to Repository: Under the settings menu, select
Repository. -
Change the Default Branch:
- Find the section labeled Default branch.
- In the dropdown menu, select
master. - Click the Save changes button to apply the new default branch.
Step 3: (Optional) Delete the Old
mainBranchIf you want to delete the old
mainbranch after you've confirmed everything is working correctly:-
You can delete the
mainbranch locally (if you don’t need it anymore):git branch -d main -
Delete the
mainbranch on GitLab:git push origin --delete main
Important Note
- Make sure that there are no active pull requests or merge requests targeting the
mainbranch before you delete it. - Inform your team about the change in the default branch to avoid confusion.
After completing these steps, your default branch should now be set to
master. -
- User
ok and make it protected
- Assistant
To protect the newly created
masterbranch in GitLab, follow these steps after you've set it as the default branch:Step 1: Access Repository Settings
- Log in to GitLab: Go to your GitLab instance and log in to your account.
- Open Your Project: Navigate to the project you want to modify.
Step 2: Protect the
masterBranch-
Go to Settings: In the left sidebar, click on
Settingsto expand it. -
Select Repository: Under the settings menu, click on
Repository. -
Locate Protected Branches:
- Scroll down to the section labeled Protected branches.
-
Protect the
masterBranch:- In the Protect a branch section, enter
masterin the branch name field. - Under the settings for who can merge into or push to this branch, choose one of the options based on your needs. You can set it so that:
- No one can push to it
- Only Maintainers can merge to it
- Developers can merge to it, etc.
- Click the Protect button to apply the protection.
- In the Protect a branch section, enter
Step 3: Confirm Protection
Once you have added the
masterbranch to the list of protected branches, it should be visible in the Protected branches section. It will show what type of restrictions have been applied (e.g., who can push to or merge into the branch).Important Notes
- Branch Protection Levels: Make sure the protection level aligns with your team's workflow. For example, you might want only Maintainers to have push access if you are enforcing strict review practices.
- Revisions: You can always edit or remove the protections later if your needs change.
- Communicate: Let your team know about the protected status of the branch so they are aware of any new restrictions regarding merges or pushes.
After completing these steps, the
masterbranch will be protected, helping to enforce good version control practices in your GitLab project.