DEV Community

loading...

How to rename the master branch on Github.

jonathanbcsouza profile image Jonathan Souza Updated on ・2 min read

Since 1st October of 2020, all the new repositories on Github have their default name defined as main.
If you have created repositories before that date, all of your branches are named as master.
GitHub and the wider Git community are gradually transitioning the default branch name from master to main and it is slowly becoming a standard within the community.
In this article, we will walk through, step by step, to learn how we can easily rename the branch's name.

image

Step 1. Select your repository

  1. On GitHub, navigate to the main page of the repository.
  2. Above the list of files, click Code.
  3. Copy the link.

image

Step 2. Clone it into your local machine

  1. Open your terminal.
  2. Change the current working directory to the location where you want to save the project.
  3. Type git clone, plus the URL you copied earlier.

image
Awesome! Now you have your repository saved in your machine.

Step 3. Modify the branch name:

Still in your terminal, access the root folder of your project with cd and execute the following:

git branch -m master main

This command will create a new branch called main and checkout into this new one. The complementary -m will move all the commit history from master into your brand new main branch.

Step 4. Remove the “master”.

Now it is the time to finally delete your old branch.

  1. Execute the following command, it will set your local machine to start monitoring the new branch. It will also update your remote Github repository. git push -u origin main
  2. Now we need to point the HEAD to the current branch reference. To do that, do the following: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
  3. At this stage, you can double-check your tree. git branch -a

image
image

4- At your Github repository, in the Setting's section, go to the branches section and switch the default branch to “main”. See the image below.

image
image

5- Go back to your terminal and delete the old branch.
It will be deleted locally (your machine) and remotely (from your Github).

git push origin --delete master

image
image

Step 5. Celebrate!

You have done such a great job! 😎
Be proud of yourself and thanks for your interest in that matter.

Discussion (0)

pic
Editor guide