How To Change Default Branch in Gitlab?
Last Updated :
16 Sep, 2024
In GitLab, the default branch is the main working branch for your project, typically set as "main" or "master". This branch serves as the primary point for collaboration, code reviews, and deployments. However, there are times when you may need to change the default branch to reflect naming conventions, feature developments, or team preferences.
Changing the default branch in GitLab can simplify your workflow and improve project organization. In this article, we will walk you through the process of changing the default branch in GitLab.
There are two primary ways to change the default branch in GitLab
1. Using GitLab Web Interface
Step 1: Navigate to Your Repository
- Log in to your GitLab account.
- Go to the project for which you want to change the default branch.
Step 2: Access the Project Settings
- On the left sidebar, click on Settings.
- From the dropdown, select Repository.
Access the Project SettingsStep 3: Locate the Default Branch Setting
Scroll down to the Default branch section.
Step 4: Select the New Default Branch
- Use the dropdown menu under Default branch to select the desired branch.
- Click on the Save changes button to apply the new default branch.
How to Change Default branch in GitlabStep 5: Verify the Change
- Check the top of the repository to confirm that the default branch has been updated.
- Ensure that the new default branch reflects across merge requests, CI/CD settings, and other relevant areas.
2. Using Git Commands
If you prefer command-line interaction, you can change the default branch by pushing the new branch and updating settings:
Step 1: Create and Push the New Branch:
If the branch does not exist yet, create it and push it to the remote:
git checkout -b new-default-branch
git push origin new-default-branch
Step 2: Set the New Branch as Default Using GitLab API:
You can use GitLab's API to set the default branch. First, ensure you have a GitLab API token with the necessary permissions.
Use the following curl command to update the default branch:
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" \
--data "default_branch=new-default-branch" \
"https://gitlab.com/api/v4/projects/<project_id>"
Step 3: Verify the Change:
Check your GitLab repository settings or use the GitLab API to verify that the default branch has been updated.
Updating Local Clones After Changing the Default Branch
Once the default branch has been changed, it’s important for all team members to update their local clones:
Step 1: Fetch the Latest Changes:
git fetch origin
Step 2: Switch to the New Default Branch:
git checkout new-default-branch
Step 3: Set the Local Default to Match the Remote
If necessary, update your local configuration to set the new branch as the default:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/new-default-branch
Step 4: Clean Up Old Branch References
If the old default branch is no longer needed, you can delete it locally and remotely:
git branch -d old-default-branch
git push origin --delete old-default-branch
Similar Reads
How to Delete Branch in Gitlab?
When working with Git, it's common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, it's often necessary to delete it to keep your repository clean and organized. In this article, we'll see the process of deleting a Git branch us
2 min read
How to Change Git Default Branch From Master?
Changing the default branch in Git from master to main has become a common practice in many development teams. This shift is part of a broader movement towards more inclusive and descriptive terminology in software development. This article will guide you through the steps required to change your de
4 min read
How To Create Branch In Git?
Creating branches in Git is a fundamental skill for any developer. Branches allow you to work on different features, bug fixes, or experiments in isolation from the main codebase. This way, you can keep your main branch stable while making changes on separate branches. In this article, weâll guide y
2 min read
How to Change Branch Name in Git?
Git is a powerful version control system widely used for managing code changes in software projects. One common task that developers encounter is renaming branches. Whether due to a change in project scope, a shift in naming conventions, or a simple typo, knowing how to rename branches efficiently i
3 min read
How to Delete Branch in Github?
In Git repositories, branches are important for organizing and managing code changes, but over time, unused or outdated branches can clutter the repository. Deleting these branches helps maintain a clean and manageable codebase. In this article, we'll explore the process of deleting branches in GitH
2 min read
How to Check Branch in Git?
In Git, branches are independent lines of development that allow you to work on features or fixes without affecting the main codebase. Checking branches is important for managing project history, coordinating collaboration, and ensuring smooth development workflows. This article will walk you throug
2 min read
How to Delete a Branch in Git?
When working with Git, itâs common to create branches to isolate changes and work on new features or fixes. However, once a branch has served its purpose, itâs often necessary to delete it to keep your repository clean and organized. In this article, weâll see the process of deleting a Git branch us
3 min read
How to Clone a Branch in Git?
Git is a popular version control system that allows developers to track changes in their code and collaborate with others. Cloning a branch in Git involves creating a copy of a specific branch from a remote repository. This article will guide you through the process of cloning a branch in Git. Table
3 min read
How To Create And Commit A Branch in GitLab?
GitLab is a popular Git repository hosting service with built-in tools for Continuous Integration/Continuous Deployment (CI/CD), issue tracking, and more. Branching is a fundamental aspect of Git workflow, allowing developers to isolate features, bugs, or experiments from the main codebase. This gui
3 min read
How to Checkout New Banch in Git?
Git provides powerful features for managing code changes, collaborating with teammates, and organizing development workflows. One essential task in Git is checking out new branches, which allows developers to work on isolated features, fix bugs, or experiment with changes without affecting the main
2 min read