The default branch in Git is the primary base of development and is typically the branch where all changes eventually merge back. This is also the branch that Git automatically checks out when you clone a repository. Originally, Git designated 'master' as the default branch name, but recent conventions and best practices encourage using alternative names like 'main'.
Steps to change the default branch in git
Locally changing the default branch
Create the New Branch (if it does not exist): As an example, let's change the default branch from "master", the outdated convention, to "main", the current naming standard.
If the new default branch does not exist, first you need to create it. This is typically done by branching off from the old default branch:
Terminalgit checkout mastergit checkout -b mainPush the new branch to remote:
Push the new branch to your remote repository:
Terminalgit push -u origin mainChange local default branch:
To change the default branch locally, you use the
git symbolic-ref
command:Terminalgit symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/mainThis command sets the HEAD of your local clone to point to 'main' instead of 'master'.
Changing the Default Branch on GitHub
For repositories hosted on GitHub, you need to update the default branch through the web interface.
Navigate to the Repository Settings:
- Go to your repository on GitHub.
- Navigate to the "Settings" page at https://github.com/<ORG_NAME>/<REPO_NAME>/settings.
Change the default branch:
- In the "Default branch" section, click the switch button next to the current default branch.
- Select the new default branch from the dropdown and confirm the change.
Changing the default branch on GitHub automatically updates new clones and existing checkouts to point to the new default branch.
Delete the old branch (optional):
After ensuring all pull requests and changes are merged into the new default branch, you can choose to delete the old default branch:
Terminalgit push origin --delete master
Changing default branch in other remote repositories
If your repository is hosted on platforms other than GitHub, like GitLab or Bitbucket, the process to change the default branch is similar but accessed through their respective settings:
- GitLab: Go to "Repository" > "Settings" > "Repository" and change the default branch under "Default Branch".
- Bitbucket: Navigate to "Repository settings" > "Branches" and change the default branch under "Main branch".
Updating local repositories after change
Once the default branch is changed remotely, team members should update their local repositories to reflect this change:
git fetch origingit branch -u origin/main maingit symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
These commands adjust the local configuration to track the new default branch and ensure that the HEAD
is correctly set.
For more information on default branches see the official documentation.