DEV Community

Jonathan Souza
Jonathan Souza

Posted on • Updated on

Renaming the Master Branch to Main On GitHub

Update: You can now rename it directly from GitHub πŸŽ‰.
Check it out: Renaming a branch on GitHub!

Original Post:
Since October 1, 2020, all new repositories on GitHub have defaulted to using main as the branch name. If you created repositories before this date, your branches will be named master. GitHub, along with the broader Git community, is gradually transitioning the default branch name from master to main, a convention that's slowly being adopted across the community. In this guide, we'll walk you through the process of renaming your branch, step by step.

Step 1: Select Your Repository

  1. Navigate to the main page of your repository on GitHub
  2. Copy the URL provided

Step 2: Clone the Repository to Your Local Machine

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

Step 3: Modify the Branch Name

While still in your terminal, navigate to the root folder of your project using cd <your-project-folder> and execute the following command:
git branch -m master main

This command creates a new branch named main and switches to it, with the -m flag transferring all the commit history from master to your new main branch.

Step 4: Remove the β€œmaster” Branch

Now it's time to remove the old branch.

Execute the following command to set your local machine to track the new branch and update your remote GitHub repository:
git push -u origin main.

Next, point the HEAD to the current branch reference with: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

You can now verify your tree using:
git branch -a

  1. Navigate to your GitHub repository, go to the Settings section, find the branches section, and switch the default branch to β€œmain”, as shown below.

  1. Return to your terminal and delete the old branch both locally and remotely with: git push origin --delete master

Step 5: Celebrate!

You've done a fantastic job! 😎

Top comments (0)