How to rename the master branch to main

While git hosting companies like GitHub are already setting the default branch to main for any new repositories since October 1st 2020, there are still a lot of repositories you own that still uses the master naming as the default branch.

For the reas…


This content originally appeared on DEV Community and was authored by Alessio Michelini

While git hosting companies like GitHub are already setting the default branch to main for any new repositories since October 1st 2020, there are still a lot of repositories you own that still uses the master naming as the default branch.

For the reason why you should do it, I’ll point to this article "Why GitHub renamed its master branch to main" which probably does a better job than what I would try to do.

But it’s a very easy process, it requires only a few commands and a setting change in the GitHub repository.

Rename master to main

First thing to do is to checkout the master branch if you didn’t already:

git checkout master
# Also be sure you have the latest changes
git pull origin master

Now you can rename the local branch with the following command:

git branch -m main

But now this change is only in your local git folder, what you need to do next is to push this the remote, which is as simple as to run this command:

git push origin -u main

Change the default branch

Now, at this point you have both master and main on your remote, and before you can delete the master branch, you need to go in the repository settings, go into the Branches section, and check what’s the default branch there.

Check default branch

If it’s master you will need to change that to your new main branch, as shown below:

Switch default branch

Once you have switched the default branch, you can safely delete the remote master branch:

git push origin --delete master

And that’s it! You can say goodbye to the master branch for this repository.
Then it’s just rinse and repeat for all your repositories.


This content originally appeared on DEV Community and was authored by Alessio Michelini


Print Share Comment Cite Upload Translate Updates
APA

Alessio Michelini | Sciencx (2022-04-21T12:47:03+00:00) How to rename the master branch to main. Retrieved from https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/

MLA
" » How to rename the master branch to main." Alessio Michelini | Sciencx - Thursday April 21, 2022, https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/
HARVARD
Alessio Michelini | Sciencx Thursday April 21, 2022 » How to rename the master branch to main., viewed ,<https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/>
VANCOUVER
Alessio Michelini | Sciencx - » How to rename the master branch to main. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/
CHICAGO
" » How to rename the master branch to main." Alessio Michelini | Sciencx - Accessed . https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/
IEEE
" » How to rename the master branch to main." Alessio Michelini | Sciencx [Online]. Available: https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/. [Accessed: ]
rf:citation
» How to rename the master branch to main | Alessio Michelini | Sciencx | https://www.scien.cx/2022/04/21/how-to-rename-the-master-branch-to-main/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.