Renaming the default branch from master to main on Github.

Overview

Based on the suggestion by the Conservancy, GitHub renamed the master branch to main branch.

To learn more about the changes, see The default branch for newly-created repositories is now main.

Steps

Rename local master branch


git branch -m master main

Copy the code

Push main to remote repo


git push -u origin main

Copy the code

Update default branch on Github

Open GitHub repository in the browser, click Settings -> Branches and change the default branch to main.

Or using GitHub REST API.


curl \

-X PATCH \

-H "Accept: application/vnd.github.v3+json" \

-H "Authorization: token {access_token}" \

https://api.github.com/repos/{owner}/{repo} \

-d '{"default_branch":"main"}'

Copy the code

Delete master branch


git push --delete origin master

Copy the code

Changing the default for new local repositories


git config --global init.defaultBranch main

Copy the code

Using github-renaming

github-renaming, renaming the default branch of our own repositories on GitHub easily.


gem install github-renaming

github-renaming default-branch <old-branch> <new-branch> -t <token> -r <user/repo>

Copy the code

References

  • Docs.github.com/cn/github/a…

  • Github.com/github/rena…

  • www.hanselman.com/blog/easily…

  • Docs.github.com/cn/rest/ref…

  • Docs.github.com/en/rest/ref…