Skip to main content

How to rename the ‘master’ branch on GitHub

First posted in Development and Git; updated 26th February 2021

So renaming master to main is a good idea, but how do we do it? It’s really easy if you’re using GitHub:

  1. Go to your repository (repo) and find the ‘Branches’ link (including a wee branch icon and the number of existing branches), or navigate to https://github.com/myUserName/myRepoName/branches
  2. In the ‘Your branches’ section, hit the pencil icon on the master line
  3. In the ‘Rename this branch’ dialog, get rid of the word master, type main, and press ‘Rename branch’

That updates the branch name centrally; next we need to update the branch name on all of the machines that connect to this repo, run three commands:

git branch -m master main
git fetch origin
git branch -u origin/main main

You can just blindly copy and paste that code block to run all three, but let’s break down what each line is doing:

  1. First we rename the local branch from master to main, to match what we’ve just done on the remote repo: git branch -m master main
  2. Next, we get the most up to date info from the remote repo (in other words, the fact that master is gone and there’s a ‘new’ main branch): git fetch origin
  3. Lastly, we set the ‘upstream’ branch to main for your local main branch, so that pushing and pulling without specifying the branch is possible git branch -u origin/main main

Subscribe

If you enjoyed reading this and want a monthly roundup of my articles delivered to your inbox, just enter your email below.

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that’s not enough, have a look at the full list.

  1. Using Pandoc to convert Markdown to Jira’s Textile in Sublime Text

    It’s a real pain that Jira’s plain text input isn’t Markdown. Here’s how to I write in Markdown and export to Jira’s version of Textile.

  2. Apple dials back the Safari 15 for macOS redesign

    The ultra-condensed tab bar in the up-coming Safari for macOS has been reverted in the betas; it’s now just an option in Safari’s Preferences.