Home » Git » ‘git rename branch’ – how to rename a git branch

‘git rename branch’ – how to rename a git branch

By Emily

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. This post will show you how to rename a local git branch using the git branch -m command and an example of exactly how to rename your git master branch to main. So to learn how to ‘git change branch name‘, read on.

Use git branch to see local branch names

Before we start renaming any branches get a list of your branches using the git branch command with no flags, as shown here:

git rename master branch to main

This shows you a list of local branches. In the list you can see in the image above that we are currently on the main branch, as it’s highlighted in green and has an asterisk next to it. You can also see that there are 3 other branches, one of which is called branch-name-now.

How to rename a local git branch

To rename a local git branch you need to use the git branch -m command (official documentation is here). You will also have to specify the name of the branch you want to rename and the name you want to rename it to. Putting all of this together you can use this command to rename a local git branch :

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

This means ‘please rename the branch which is called branch-name-now to new-branch-name‘.

Now use the git branch command to list your local branches again. You can see in the image above that I am still on main, there are still 3 other branches, but we now have a branch called new-branch-name instead of branch-name-now.

How to Git rename master branch to main

Some git 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 new-branch-name

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

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

What are Git branch -m and –move

What is the -m flag in this command? :

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

The -m just stands for move. And -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.

Summary

You now know how to git change branch name, how to rename a local git branch, and how to rename the git master branch to main using the git branch -m command.