How to Renaming GitHub Master Branch to Main Branch?

How to Renaming GitHub Master Branch to Main Branch

Introduction

GitHub is one of the most useful tools in a developer’s arsenal today, with several organizations using Git to control and deploy code. Recently, GitHub has implemented the Conservancy’s suggestion and has stopped using the term master when a new repository is initialized. Now, the master branch is called the main branch, and GitHub stated that they are encouraging projects to switch to branch names that are meaningful and inclusive while making it easier for them to use the new default name. This was carried out after the 2020 protests when the term master was found to be offensive to people.

If you’re looking to change the name of your GitHub branch as well, then this blog will help you accomplish just that in a few simple steps:

Guide to rename branch

Note: People with write permissions to a repository can rename a branch in the repository. People with admin permissions can rename the default branch.

1. First clone repository to your local.

git clone repo_url

2. Checkout branch which you want to rename. For example, “master” to be renamed to “main”.

git checkout master

3. Move your “branch_name_to_be_renamed” to “new_branch_name” –

git branch -m master main

4. Push your new branch name to GitHub repo

git push -u origin main

(Here -u sets the new branch as the local default at the same time)

git remote set-head origin main

(This line ensures our local HEAD points to our new branch on GitHub)

5. Verify branches and you can find origin/HEAD -> origin/main

git branch -r

6. Go to GitHub and change your default branch to the newly created branch. If it is not the master or default branch skip this step. To achieve this, go to your repository settings > Branches and select the default branch.

GitHub Default Branch Change

7. Delete old branch. Before taking this step go to  your repository on GitHub and verify your new branch appears in the list.

git push origin –delete master

8. In this step you should have to verify your code, scripts, automation, integrations for references to the master branch. For example, check files like: travis.yml, .github/workflows/, .circleci/config.yml, etc. and make sure there are no external services or links relying or referencing the master branch.

9. Git does not override some native commands like git init, soyou can create git new using following command:

git config –global alias.new ‘!git init && git symbolic-ref HEAD refs/heads/main’

Tip: While you are renaming other branches than set your origin to the default branch. This can be achieved using step 8, just change the default branch name.

What team must follow

Within team if they have local clone of repository, they must perform following steps:

  1. Checkout master branch: git checkout master
  2. Rename it too main: git branch -m master main
  3. Get latest commits: git fetch
  4. Remove existing tracking connections: git branch –unset-upstream
  5. Create new tracking connections: git branch -u origin/main

Things to Keep in Mind

Renaming a branch is not terrifying or complex.

You should always backup your repository for recovery.

Wrapping Up

This guide explained how to successfully rename your branch to a more creative and inclusive name as per GitHub and the Conservancy’s advice. By following all the steps mentioned above, you should be able to accomplish this without any trouble. However, if you have any queries about GitHub or related topics, feel free to get in touch with our DEV IT engineers for the info.

You May Also Like

The following two tabs change content below.
Ronak Pandya has 14 years of experience in IT Infrastructure, Project Management, and motivational speaker in highpowered world of IT. He has expertise in CloudOps (AWS, Azure), DevOps (Jenkins, GitHub, Jira), Platform (Windows, Linux), Database (MySQL, MSSQL, PostgreSQL). He also spares his time with family and write poetries. He believes in "Think Differently".

Leave a Reply

Your email address will not be published.