Git branch rename - Command to change a branch name in Git

There’s really no magic when it comes to renaming a Git branch locally and remotely.

There isn’t an actual git rename branch command, but the –move switch of the git branch command achieves the required results.

How to rename a git branch

The steps to rename a git branch name are:

  1. While on the Git branch to rename, issue the git branch -m new-branch-name command
  2. Push the new branch to your remote repository
  3. Delete the branch with the old name from your remote repo

Git branch rename example

In my own local environment, I have a branch named bogfix which I need to rename to bugfix:

branch@rename  /c/git/gitub (bogfix)
$ git branch -a
* bogfix
main

To rename the Git branch bogfix to bugfix, I simply use the -m switch with the git branch command and provide a new branch name.

branch@rename  /c/git/gitub (bogfix)
$ git branch -m bugfix

A quick request to show all branches shows that the command to change the Git branch name was successeful, as the bogfix branch was renamed to bugfix.

branch@rename  /c/git/gitub (bogfix)
$ git branch -a
* bugfix
main

Git branch rename command

The formal syntax of the git branch rename command is as follows:

git branch (-m | -M) [<oldbranch>] <newbranch>

If it is the current branch being renamed, the oldbranch property defaults to the name of the current branch.

You’ll notice the switch used to rename a Git branch is -m or -M. That’s because the actual command is move, not rename.

The uppercase -M switch will issue the move command with force, which may be necessary if the Git branch to rename already exists.

Rename the remote Git branch

If the renamed Git branch originated from a remote server like GitHub, BitBucket or GitLab, then the incorrectly named branch on the server must be renamed too. The easiest way to do this is to simply delete the incorrectly named branch on the server and push up the renamed branch.

A remote branch can be deleted using the vendor’s online tools, or through the terminal window with the following command:

branch@rename  /c/git/gitub (bogfix)
$ git push origin --delete bogfix
To https://github.com/learn-git-fast/git-branch-examples.git
- [deleted] bogfix

Then you can push the renamed Git branch remotely, and nobody will be wise to the fact that an incorrectly named branch ever existed:

branch@rename  /c/git/gitub (bogfix)
$ git push origin -u bugfix
* [new branch] bugfix -> bugfix
Renamed git branch set up to track remote branch 'bugfix' from 'origin'.

And that’s how easy it is to rename a Git branch locally and remotely.

rename git branch GitHub

After you issue the git branch rename command locally, you must push the renamed branch remotely and delete the branch with the old name.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close