DEV Community

Cover image for Quickly move from "master" to "main" branch at Github
Helder Burato Berto
Helder Burato Berto

Posted on • Originally published at helderburato.com

Quickly move from "master" to "main" branch at Github

On this article, I'll show how to update your default branch from "master" to "main".

Why do this?

I really recommend you look for more about and take your own decisions about, in this article I'll focus only on changing the default branch and don't go deeper into the motivation about it.

The new repositories created on Github will be set to "main" as default, it started October 01st, read more about it.

Set git init to "main"

To set as default to initialize repositories with main you can do the following:

$ git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode

Or add to your .gitconfig like this

Rename Branch

Let's rename your current "master" to "main" branch with the following code:

$ git branch -m master main
Enter fullscreen mode Exit fullscreen mode

tl;dr

Rename a branch (must not have it checked out to do this):
git branch -m old_branch_name new_branch_name

After rename, push to the remote origin:

$ git push origin main
Enter fullscreen mode Exit fullscreen mode

You'll see something like that:
Screen Shot 2020-10-05 at 8.59.24 AM

Github: Settings

  1. Go to your repository at Github and access the Settings tab https://github.com/<username>/<repository>/settings
  2. Choose Branches on the sidebar
  3. Update from master to main like the following image: Screen Shot 2020-10-05 at 9.03.47 AM
  4. It will show an alert modal, you just need to accept then: Screen Shot 2020-10-05 at 9.04.23 AM
  5. Now, if you go to the root link at your repository you'll see the current default branch: Screen Shot 2020-10-05 at 9.05.13 AM
  6. Notice has 2 branches in this case, one is the master, you can delete with the following code:
$ git push --delete origin master
Enter fullscreen mode Exit fullscreen mode
  1. Tadah! Now you have just the main and deleted the master branch. Screen Shot 2020-10-05 at 9.06.59 AM

Wrapping Up

I updated some of my personal repositories and I think it can be helpful for some people.

If it helped you in some way, feel free to comment below or share the article.

Enjoy Programming! :D

Top comments (0)