Git Branch Rename Tutorial | Best Practices and Commands

Recently all the new GitHub repositories are coming with the default branch main. This is not liked by all the developers around the world. So in this tutorial I am going to walk you through how to rename the master branch to main in Git. In other words I will show you how to do the git rename branch.

Why do we do the "Git rename branch"

There isn’t anything fancy, it is just to demo how we can rename a branch in Git.

Renaming the Local master Branch to main

The first step is to rename the “main” branch in your local Git repositories:

$ git branch -m master main

Renaming the Remote master Branch as Well

Since we have renamed the local branch to master, make a commit and push the changes to remote:

$ git push -u origin master

We now have a new branch on the remote named “master”. Let’s go on and remove the old “main” branch on the remote:

$ git push origin –delete main

Again try to delete and see if it deletes or not:

$ git push origin –delete main
error: unable to delete ‘main’: remote ref does not exist
error: failed to push some refs to ‘https://github.com/jeenus/test.git’

Leave a Reply

Your email address will not be published. Required fields are marked *