Rename git master branch to main

Published at 1 April 2021


How to rename old repository master branch to main.

Rename local branch

$ git branch -m master main

Push new main branch to origin

$ git push -u origin main

Delete old master branch

If old master isn’t the defaultbranch on the remote

$ git push origin --delete master

If you get an error, this step needs to be done manually on the remote, as main needs to be set as defaultbefore deleting the old master.

Error message:

To github.com:<account>/<repo name>.git ! [remote rejected] master (refusing to delete the current branch: refs/heads/master) error: failed to push some refs to 'github.com:<account>/<repo name>.git'