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
  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

Get them delivered!

If you enjoyed this and want all the latest articles delivered to your inbox every month, pop your email in the form 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. The difference between strikethrough and del

    Just like <em> and <i>, and <strong> and <b>, the distinction between <s> and <del> is subtle, but it’s worth knowing.

  2. Git restore to discard changes

    I no longer use git checkout to switch branches; I’ve also stopped using it to discard uncommitted changes: let me introduce git restore!