Have you ever wondered whether there’s a difference between the “main” branch and the “master” branch of repositories in GitHub?

Madhavi Imashi
6 min readAug 14, 2021

Well! You may sometimes know what I mean.

But, in case you’re wondering, I thought of sharing what I learned after a small study I did on that, hoping it will be useful and worth it for someone out there to be aware of it as it was for me.
Recently, I was wondering how come the default branch name of all the repositories is not the same. Since the default branch of a repository is automatically created once a new repository is created, it can’t be different, right?

OK, wait! Before talking about that, let's recall: What is the default branch of a repository?

When you create a repository with content on GitHub, GitHub creates the repository with a single branch. This first branch in the repository is the default branch. The default branch is the branch that GitHub displays when anyone visits your repository. The default branch is also the initial branch that Git checks out locally when someone clones the repository. Unless you specify a different branch, the default branch in a repository is the base branch for new pull requests and code commits.

Git and GitHub use the term master for the default version of a source code repository. Developers fork a version of the master to create secondary versions, add their code to this default version, and then merge their changes back into the master.

Sounds familiar, right?

Anyway, things have changed… Now,

Many communities, both on GitHub and in the wider Git community, are considering renaming the default branch name of their repository from master. GitHub is gradually renaming the default branch of our own repositories from master to main.

So when I checked some different repositories to determine how far it’s true for now, I figured out that the default branch of any newly created/initialized repository is coming as main.

Does GitHub notify us about this?

Of course. It does! Once you choose the ‘Add a README file’ option when you create a new repository, you will be able to see the system notify us that the default branch will be set as main.

Then the repository will be created with an initial commit in the main branch, which is automatically generated by GitHub as below.

Yes! Previously, the name of the default branch of any repository was “master”. But from October-2020 onward, the default branch of any GitHub repository is using “main” as the default branch name, instead of master.

But Nope. It’s not mandatory to keep it that way…

You can change the name of the default branch which will be appeared in the repositories that you make in the future as “master” or some other name that you want to have.

It’s simple!!

Go to Account settings > Repositories > update the name of the default branch.

Once you hit the update button, you will see a successful message appeared on the top of the page as below;

You can also change the name of the default branch of any existing repository without much effort!

Just follow the following steps and you will get it done simply.

Example: Let's try to update the default branch name of a repository from “main” to “master”!

  • Open the git bash terminal from the correct folder location of your local repository.
  • First, checkout to that branch which you want to rename. According to this example, I will checkout to the ‘main’ branch.
  • Then run the following command to create a new branch called ‘master’ which will get an exact copy of the ‘main’ branch so nothing will get lost. (Because -m will transfer the whole commit history of the ‘main’ branch into our new ‘master’ branch)

git branch -m main master

  • Now that we have successfully changed the default branch name of our local repository, it’s time to push the new ‘master’ branch to GitHub also because there should be a remote branch with the same name to track by the local branch. Therefore, run the below command;

git push -u origin master

  • Even though now we have created a ‘master’ branch in GitHub, our local HEAD is still pointing to the ‘main’ branch. Therefore, to ensure that the local HEAD points to our newly created ‘master’ branch on GitHub run the command given below.

git remote set-head origin master

  • Yay. You are almost done!. If you are not sure what happened, you can run the below command to make sure that the current HEAD is pointing now to the ‘master’ branch that we created a few moments ago.

git branch -a

  • All the above-mentioned steps are demonstrated in the following screenshot;
  • At this point, you’ve transitioned everything from the old branch to the new ‘master’ branch successfully.
  • But still, there’s a little thing to do that doesn’t require the command line. Hence, in GitHub, the default branch will still appear as ‘main’, change it to ‘master’. Go to Settings>Branches> under “Default branch”, click the double arrow icon and select ‘master’ (the branch that you want to make as the default) from the drop-down and click the update button.
  • Now that you have changed the default branch name in both the local machine and remote server successfully, it’s time to delete that old ‘main’ branch which has nothing to do with your repository anymore.

git push origin --delete main

Now everything will be as you expected. Refresh the GitHub page, and you will see that the old ‘main’ branch is no longer available and the ‘master’ branch is the default branch.

Yeah. That’s it!

Note: Anyway, the name of the default branch is not a crucial matter to think about. So unless there’s a good reason, changing the name of a default branch or switching the default branch to another is not essential.

Resource materials that you can also refer to get more information:

--

--