DEV Community

Cover image for Git 101: Rename default branch from master to main
Saravanan Gnanaguru
Saravanan Gnanaguru

Posted on • Updated on

Git 101: Rename default branch from master to main

How to rename master to main in Github

Table of Contents

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
Enter fullscreen mode Exit fullscreen mode

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'.
Enter fullscreen mode Exit fullscreen mode

Step 3 Swap default branch setting in Github UI

  • Now we have to swap the default branch from master to main, 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 choose branches from left side pane

  • Choose the branch swap icon to change the branch
    Change def branch

  • It will show the list of branches
    Image description

  • Choose main here and Click on update
    Image description

  • It will show a warning for changing the default branch name, so accept the warning to confirm
    Image description

  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
# Rename it to "main":
$ git branch -m master main

# Get the latest commits and updates from the remote:
$ git fetch
Enter fullscreen mode Exit fullscreen mode
# 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
Enter fullscreen mode Exit fullscreen mode

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

References

Git Docs
Reference Article

Community and Social Footprints

Top comments (0)