How to migrate master branch to main in Github?

Few months back, Github changed the default branch from master to main. Due to this change, most of the projects were forced migrate from master to main branch. Recently I faced this problem for one of my projects.

After multiple trials and errors, I successfully migrated my project to main branch. I would like to share the steps and it would help you when you face the similar problem in the future.

1: Connect local repo with your remote repo

git remote add origin <remote_github_url>

2: Pull the main source code

git pull origin main

3: Move the source from master to main branch

git branch -m master main

4: Add and commit your changes

git add .
git commit -m "master branch changed to main"

5: Set upstream and push changes to remote

git push --set-upstream origin main

Now all your changes will be available in the remote main branch.

NOTE: Backup your source code before you do all these changes.

23