From 'master' to 'main': The easy guide to renaming your Git default branch for a more inclusive codebase
Tags: programming, tutorial, github, inclusion
Published: 2023-03-24 11:52:06

I started heavy development on [Firecode.io](https://firecode.io) this week to get it out of beta - and one of the first changes I had to make was renaming my primary branch to "main". In 2020, Github announced that it would be dropping the use of the term "master" for the default branch name on its platform. This decision was made in response to calls for more inclusive language in tech, as "master" has historical connotations of slavery and domination. The change has been supported by many in the tech industry who recognize the importance of taking small but significant steps towards creating more inclusive spaces for everyone.

Since Github is one of the most popular code hosting platforms, this change has had a ripple effect across the tech industry. Many companies and organizations have followed suit and made the switch from "master" to "main" for their default branch names. This has involved going through a series of steps to rename the branch, update links, and make sure that the change is reflected in all relevant places.

Making this change is actually quite simple and should only take a couple of minutes:

## Step 1: Create a new branch

The first step is to create a new branch called `main` from the existing `master` branch.

```bash
git branch -m master main
```

This command renames the `master` branch to `main`.

## Step 2: Push the new branch to the remote repository

Next, you need to push the new `main` branch to the remote repository:

```bash
git push -u origin main
```

This command pushes the new `main` branch to the remote repository and sets up tracking so that future `git push` and `git pull` commands work as expected.

## Step 3: Point the HEAD to the new branch

The next step is to point the HEAD to the `main` branch. You can do this using the following command:

```bash
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
```

This command is used to update the HEAD reference in the Git repository to point to the new default branch "main" after renaming the "master" branch. In Git, the HEAD reference points to the current branch or commit that is being worked on. By default, the HEAD reference points to the current branch, which is usually the default branch. In this case, since we renamed the default branch from "master" to "main", we need to update the HEAD reference to point to the new default branch "main". The first argument is `refs/remotes/origin/HEAD`, which is the remote tracking branch reference that corresponds to the default branch on the remote repository. The second argument is `refs/remotes/origin/main`, which is the new default branch name we want to set the reference to.

## Step 4: Change the default branch on the Github

Now that you've created and pushed the new `main` branch, you need to change the default branch on the Github (or BitBucket or wherever you host your repo). Depending on where you host your repo, this should be a simple change from the web UI.

## Step 5: Delete the old branch

Finally, you can delete the old `master` branch:

```perl
git push origin --delete master
```

And that's it! You've successfully renamed the default branch of your Git repository from `master` to `main`.

It's worth noting that this change may affect some build systems or scripts that rely on the `master` branch name, so you may need to update them accordingly. Additionally, if you're using other Git-based tools, you may need to update them to work with the new `main` branch. In my case, I had to rename all my terminal aliases and update multiple cloud build scripts to pull the right code.

The shift towards more inclusive language in tech is an important one, and every small step taken towards creating more welcoming and equitable spaces is a step in the right direction.
___

_Preparing for a coding interview? Check out [Firecode.io](https://firecode.io)_