Skip to content

Easily rename your Git default branch from main to master

Posted in Tutorials

About a year ago, Scott Hanselman wrote a post on how to easily rename your git default branch from master to main. I took some inspiration from that today after noticing a bunch of code hosting services like Gitlab and Github zealously decided to name default branches for new projects main. By inspiration I mean I copied the commands from Scott’s post and flipped words since this is what this is about. Well, among other things but I have more words on that. Now you will also have a way to easily rename your Git default branch from main to master.

Did you surprisingly start a new project with no master branch? Did you find in its righteous place the overzealous main branch? Or maybe you realised that since Git developers introduced the ability to change the default branch in the 2.28 release and the coding wokelords ran with it. You know, because everything gotta be woke now. However, if you want to revert to the old ways, you are in luck.

People say we should not gatekeep things, especially around comic books and software development. However, I think gatekeeping can be a good thing. Something feeling threatened by a branch named master may not be the most productive colleague you can get. If you feel some sort of racial distress about slavery you never experienced. If you use that to coerce others’ way of working, you are not interested in software development. Just stay far far away from it. Clear and simple. But, since you are here, I guess you are not this person and are worthy of learning from this post.

First, new project creation. You can set the name of the default branch with the command below:

git config --global init.defaultBranch master

However, you may be working on something and not have had time to fix your branch naming. Maybe you didn’t want to look into the git client changes and didn’t want to risk messing your repository. In that case, you can use the move command below:

git branch -m main master
git push -u origin master

The -m or --move parameter move will preserve your reflog, meaning that your history is preserved and you can delete that ridiculous main branch.

Now, what if someone had a clone of that repo before you fixed it, give them this:

git checkout main
git branch -m main master
git fetch
git branch --unset-upstream
git branch -u origin/master
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master

If only all these first-world problems had solutions as easy as this we’d reach peak humanity. Until then you’ll have to keep reading this blog. Now that you know how to easily rename your Git default branch from main to master you may go. See you next time.

Cover by Abby Chung from Pexels

Be First to Comment

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    %d bloggers like this: