Git checkout main - How to change the default branch of your project
Changing the default branch of your project on Github is simpler than it seems. You will need to run a couple command on the terminal, but if you are using Gitlab, you can even use only the web UI. Let’s start with Github.
Creating the main branch
# Start by cloning the repository on your computer
$ git clone [email protected]:<user>/<project>
# If you already have the project locally, go to the master branch and update it
$ git checkout master
$ git pull
# Create a new branch called "main"
$ git checkout -b main
# Sent the new branch to Github
$ git push -u origin main
Updating Github
To update Github, go to the branches settings webpage of your project at https://github.com/<user>/<project>/settings/branches. In the first section, Default branch, choose the new one we just created and update the repository. Just below in the next section, if there are any existing protection rule for the master branch, don’t forget to update to main.
To delete the master branch, access the branches list of your project at https://github.com/<user>/<project>/branches and click on the red bin button.
What about Gitlab?
For Gitlab, you can do the same steps by using their web app:
- To create a new branch, go to https://gitlab.com/<user>/<project>/-/branches/new
- To change the default branch, go to https://gitlab.com/<user>/<project>/-/settings/repository
But… why?
@Una Kravets said perfectly in a tweet:
o/