How to rename master to main in Github
Table of Contents
- Context setting
- Step 1 Rename master to main in local repo
- Step 2 Rename the branch master to main in remote
- Step 3 Swap default branch setting in Github UI
- Step 4 Finally delete the master branch from remote
- Steps to rename the other users local repo
- Conclusion
- References
Context setting
As part of adopting the Open Source Inclusive Naming Initiative
, it is recommended to change the default branch name from master
to main
in Source Code Management tools.
So recently I've renamed one of my repo's default branch from master
to main
. This blog captures the steps to rename the branch and change the default branch name to new renamed branch.
Renaming of branch can be done by Git Admins. Otherwise user has to be the owner of the repo or collaborator with appropriate permission to perform the branch renaming.
Step 1 Rename master to main in local repo
- As per Git branch documentation, we can use
git branch -m
command to rename the branch
$ cd <local repo dir>
$ git branch -m master main
Step 2 Rename the branch master to main in remote
- Remote repo is the repo stored in SCM server
- After we changed the name of repo, we need to push the changed repo name to remote using
git push -u
command
$ git push -u origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'main' on GitHub by visiting:
remote: https://github.com/chefgs/terraform_repo/pull/new/main
remote:
To github.com:chefgs/terraform_repo.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Step 3 Swap default branch setting in Github UI
- Now we have to swap the
default
branch frommaster
tomain
, so we can permanently delete the old branch name, to avoid any future confusions Follow the steps below to swap the default branch name in GitHub SCM.
Go to repo
settings
tab and choosebranches
from left side paneIt will show a warning for changing the default branch name, so accept the warning to confirm
So the default branch name is changed to
main
Users needs to follow the SCM tool specific steps to change the default branch name
Step 4 Finally delete the master branch from remote
- Now we changed the default branch name to
main
. So delete the master branch from remote repo
$ git push origin --delete master
To github.com:chefgs/terraform_repo.git
- [deleted] master
Steps to rename the other users local repo
- In case if the same repo is checked out and used by other people or team members, then following the steps below will be useful for changing the users local branch to main
$ cd <repo dir name>
# Switch to the "master" branch:
$ git checkout master
# Rename it to "main":
$ git branch -m master main
# Get the latest commits and updates from the remote:
$ git fetch
# Remove the existing connection with "origin/master":
$ git branch --unset-upstream
# Create a new tracking connection with the new "origin/main" branch:
$ git branch -u origin/main
Conclusion
Hope this blogs is helpful to know the commands and steps involved in renaming master branch to main.
Post your queries in my social handles
Top comments (0)