DEV Community

Cover image for Rename the branch from "master" to "main" in an existing GitHub repository
welchi
welchi

Posted on

Rename the branch from "master" to "main" in an existing GitHub repository

What I want to do

Sometimes I want to change the name of a branch, both locally and remotely.
For example, I may want to change the name of the branch from master to main.
This article will show you how to do that.

Procedure

First, locally, rename the branch from master to main.

git branch -m master main
Enter fullscreen mode Exit fullscreen mode

Next, you'll add the main branch to the remote.
スクリーンショット_2020-10-13_23_48_51eng

You can also set the main branch as the default branch.

スクリーンショット_2020-10-13_23_51_133eng

Then delete the master branch in the remote.

git push origin :master
Enter fullscreen mode Exit fullscreen mode

Finally, the local main branch follows the remote main branch.

git push --set-upstream origin main
Enter fullscreen mode Exit fullscreen mode

References

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

Top comments (0)