Since 1st October of 2020, all the new repositories on Github have their default name defined as
If you have created repositories before that date, all of your branches are named as
GitHub and the wider Git community are gradually transitioning the default branch name from
main and it is slowly becoming a standard within the community.
In this article, we will walk through, step by step, to learn how we can easily rename the branch's name.
- On GitHub, navigate to the main page of the repository.
- Above the list of files, click Code.
- Copy the link.
- Open your terminal.
- Change the current working directory to the location where you want to save the project.
git clone, plus the URL you copied earlier.
Still in your terminal, access the root folder of your project with cd and execute the following:
git branch -m master main
This command will create a new branch called
main and checkout into this new one. The complementary
-m will move all the commit history from master into your brand new
Now it is the time to finally delete your old branch.
- Execute the following command, it will set your local machine to start monitoring the new branch. It will also update your remote Github repository.
git push -u origin main
- Now we need to point the HEAD to the current branch reference. To do that, do the following:
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
- At this stage, you can double-check your tree.
git branch -a
4- At your Github repository, in the Setting's section, go to the branches section and switch the default branch to “main”. See the image below.
5- Go back to your terminal and delete the old branch.
It will be deleted locally (your machine) and remotely (from your Github).
git push origin --delete master
You have done such a great job! 😎
Be proud of yourself and thanks for your interest in that matter.