How to rename the ‘master’ branch on GitHub
First posted 25th February 2021 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:
- 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
- In the ‘Your branches’ section, hit the pencil icon on the
master
line - In the ‘Rename this branch’ dialog, get rid of the word
master
, typemain
, 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:
- First we rename the local branch from
master
tomain
, to match what we’ve just done on the remote repo:git branch -m master main
- 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
- Lastly, we set the ‘upstream’ branch to
main
for your localmain
branch, so that pushing and pulling without specifying the branch is possiblegit branch -u origin/main main
Don’t forget that anything you’ve got that watches for changes to your master
branch, like Netlify build hooks, will need to be updated to watch main
instead.