In this tutorial, I want to show you how to rename your master branch in Git, or GitHub specifically, to something other than master. I am going to show you how to do this with the GitHub website as not everyone likes using the command line. If you want to use the command line, then Scott Hanselman has a great tutorial on how to do that.
The idea of renaming the master branch in Git has come about after the recent Black Lives Matters protests in 2020. It is about removing any references to slavery with the terms master used as the default branch name. This tutorial isn’t here to argue whether you agree with this or not. If you don’t agree, then you can stop reading. If you do agree, then this tutorial will show you how to do the rename without using the command line, and it is straightforward to do.
First of all, load up your repository in GitHub, so you are on the main page for that repository. Then click on the ‘Branch: master’ dropdown box.
Then in the text box type in the name of the new branch you want to use. In my case I called it ‘main’, but you can call it anything you want. When you have typed in a name, click the ‘Create branch: main from master’ button as shown in the screenshot.
Then on your main repository page, you will see you have two active branches (or more branches if you have created feature or release branches). At this point, you have created a new branch called ‘main’ that duplicates the master branch, so it contains the entire commit history, but ‘master’ is still the default branch.
From your main repository screen, click on the branches tab again. Next to the ‘master’ branch, there is a button that says, ‘Change default branch’. Click this button.
This will bring up the default branch screen as you can see above. Select the newly created main branch.
You will receive a warning; if you are happy to proceed then click the ‘I Understand’ button. If you have a build pipeline setup to build your code and run tests automatically, this will be most likely to break at this point.
Now ‘main’ is the default branch, but the old master branch is still around as a secondary branch, but you can delete it now if you wish. You can do this by hitting the little red trash can icon next to the master branch.
Once you have done that you will only see the new ‘main’ branch. If you don’t have any automatic builds setup with something like GitHub Actions, Travis, Azure Pipelines, or your build system of choice them your new branch is good to go. You need to reclone that branch to your work machine and carry on working. In the example repository I used for this tutorial; I had a Travis build setup.
To remedy the build process, I logged into my Travis dashboard, and I had to click on the ‘More Options’ drop-down menu for a build and select ‘Trigger Build’.
This brings up the ‘Trigger a custom build’ window. In the branch text box, type in the name of your new branch, in this case, ‘main’, and then select that branch. Then press the ‘Trigger custom build’ button, and this will change the build trigger to look for changes on the main branch.
And, that’s it. It is a little more convoluted than using the command line, but not everyone likes using the command line, so if that’s the case these instructions should see you through.