Home » Git » How to rename a git branch

How to rename a git branch

What is git branch rename? Is that even a real git command? No it isn’t, but it is pretty easy to rename a git branch and this post will show you how. I’ll also have a detailed example of exactly how to rename your git master branch to main. Look at this screenshot of my Git bash terminal below. It shows a series of git commands that start with listing all my local branches, and then renaming one of them. The full description of each part is below.

git rename master branch to main
  1. I first typed git branch which showed me a list of local branches. In the list you can see we are currently on the main branch, and that there are 3 other branches, one of which is called branchNameNow.
  2. Next I typed git branch -m branchNameNow newBranchName which is saying ‘please rename the branch which is called branchNameNow to newBranchName‘.
  3. Finally I typed git branch to again list my local branches. Now you can see I am still on main, there are still 3 other branches, but we now have a branch called newBranchName instead of newBranchName.

Git rename master branch to main

Some systems still name the initial git branch master but it’s pretty common in other systems for the initial branch to be called main, which is my preference. If you want to rename your master branch to main then run this git command:

git branch -m master main 
git push --set-upstream origin main 

The first line renames the local master branch to main, the second line pushes new main branch to the remote repo.

The first line uses this generic command form:

git branch -m branch-name-now new-branch-name

… which renames the branch which is called branch-name-now to new-branch-name. This is exactly the same as:

git branch --move branch-name-now new-branch-name 

… because -m and –move do exactly the same thing. Please note there is only 1 dash before the ‘m’ and 2 dashes before the ‘move’. It’s also worth noting that branch names are case sensitive. I normally keep all branch names lowercase to avoid causing confusing issues when trying to find a branch, and use hyphens to make the names readable.

So that explains how to rename your git master branch to main, and covers some general info on how to rename a git branch.