iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
⛳

How to Rename the master Branch to main in an Existing GitHub Repository

に公開

What we want to achieve

Sometimes you may want to change a branch name both locally and remotely.
For example, when you want to rename a branch from master to main.
In this article, I will introduce the steps to achieve this.

Steps

First, change the branch name from master to main locally.

# Change the branch name locally
git branch -m master main

Next, add the main branch to the remote.

Also, set the main branch as the default branch.

After that, delete the master branch on the remote.

# Delete the master branch on the remote
git push origin :master

Finally, make the local main branch track the remote main branch.

# Set the local main branch to track the remote main branch
git push --set-upstream origin main

References

rename git branch locally and remotely
Git Set Upstream | Learn Version Control with Git

Discussion