Renaming the Master Branch to Main On GitHub
Tags: github
Published: 2021-04-11 12:28:48

**Update:** You can now rename it directly from GitHub 🎉. 
Check it out: [Renaming a branch on GitHub](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/renaming-a-branch)!

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chfo7dvjmani0nxa8jr7.png" width="400">

**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.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtk61dqisixmsr1bc6c0.png" width="400">

### Step 1: Select Your Repository

1. Navigate to the main page of your repository on [GitHub](https://github.com)
2. Copy the URL provided

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2p8i1vjcsjw9jje3sm7.png" width="400"> 

### 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.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btchyrw19nt8ybbwyfze.png" width="400">

### 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.  

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sk50z7zmvg5w66owc0z9.png" width="400">

### 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`

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j14pg5l3agasjddhudaq.png" width="400">

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

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9fu5c5i3cp3qk03m4kfx.png" width="400">

4. Navigate to your GitHub repository, go to the Settings section, find the branches section, and switch the default branch to “main”, as shown below.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g64754rfrkry5f6eqpwq.png" width="400">

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugc7gqkfmftkqwcxxg6d.png" width="400">

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

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3on5dlry02vaidb141y.png" width="400">

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0t8pyiirof1srgkix9n.png" width="400">

### Step 5: Celebrate!
You've done a fantastic job! 😎