Git 101: Rename default branch from master to main
Tags: git, tutorial, github, beginners
Published: 2022-06-22 10:13:09

## How to rename master to main in Github

## Table of Contents
- [Context setting](#context-setting)
- [Step 1 Rename master to main in local repo](#step-1-rename-master-to-main-in-local-repo)
- [Step 2 Rename the branch master to main in remote](#step-2-rename-the-branch-master-to-main-in-remote)
- [Step 3 Swap default branch setting in Github UI](#step-3-swap-default-branch-setting-in-github-ui)
- [Step 4 Finally delete the master branch from remote](#step-4-finally-delete-the-master-branch-from-remote)
- [Steps to rename the other users local repo](#steps-to-rename-the-other-users-local-repo)
- [Conclusion](#conclusion)
- [References](#references)

## Context setting
As part of adopting the Open Source [`Inclusive Naming Initiative`](https://inclusivenaming.org/word-lists/tier-1/), 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](https://github.com/chefgs/terraform_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](https://git-scm.com/docs/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 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n84qbej5qxt7bmacqcvp.png)

- It will show the list of branches 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef7f06n9b71klae1mmnk.png)

- Choose main here and Click on update
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p3su6121s6jvxh88net.png)


- It will show a warning for changing the default branch name, so accept the warning to confirm
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5s9p6izhfbjswfjokusd.png)

- 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

## References
[Git Docs](https://git-scm.com/docs/git)
[Reference Article](https://www.git-tower.com/learn/git/faq/git-rename-master-to-main/)

## Community and Social Footprints
- [Dev](https://dev.to/chefgs)
- [GitHub](https://github.com/chefgs)
- [LinkedIn](https://www.linkedin.com/in/saravanan-gnanaguru-1941a919/)
- [Twitter](https://twitter.com/saransid)
- [gsaravanan.dev](https://gsaravanan.dev)