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<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<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:

But… why?

@Una Kravets said perfectly in a tweet:

Reasons for changing master to main. 1. “Main” is shorter! Yay brevity! 2. It’s even easier to remember, tbh 3. If it makes any of my teammates feel an ounce more comfortable, let’s do it! 4. If it prevents even a single black person from feeling more isolated in the tech community, feels like a no brainer to me!