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 as follows:

  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 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

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 confirms that the command to change the Git branch name succeeded, and renamed the bogfix branch to bugfix.

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

Git branch rename command

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

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

If you are renaming the current branch, 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 issues 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 originates from a remote server such as GitHub, BitBucket or GitLab, then you must rename the incorrectly named branch on the server too. The easiest way to do this is to simply delete the incorrectly named branch on the server and push up the renamed branch.

You can delete a remote branch 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
- [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