DEV Community

nabbisen
nabbisen

Posted on • Updated on

Git: Rename default branch from master to main on existing repositories

Rename: master -> main

I renamed default branch of all my existing repositories in Github and Gitea several months ago.
Here shows how I did it.

Tutorial

1. Rename brnach

Clone repository.

$ git clone <repo>
$ cd <repo>
Enter fullscreen mode Exit fullscreen mode

Rename it.

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

Well, git branch's official documentation says:

With a -m or -M option, will be renamed to . If had a corresponding reflog, it is renamed to match , and a reflog entry is created to remember the branch renaming. If exists, -M must be used to force the rename to happen.

Optionally, it's able to check the result with git status.

$ git status
On branch main
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Push the change.

$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Done.

2. Configure hosting service

With Web browser, visit github or gitea, and show settings page.
Then, switch "Default branch" from master to main.

default branch is main now

3. Delete old branch

Delete old branch if it has become unnecessary.

$ git push origin --delete master
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)