How to rename and change a Git branch name locally & remotely

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

Git Branch Rename Command

The steps to change a git branch name are:

  1. Rename the Git branch locally with the git branch -m new-branch-name command
  2. Push the new branch to your GitHub or GitLab repo
  3. Delete the branch with the old name from your remote repo

Example of a Git branch renaming

On my own local environment, I had a bugfix branch incorrectly named bogfix:

[email protected]  /c/git/gitub (bogfix)
$ git branch -a
* bogfix
main

While on the bogfix branch, the command to change the Git branch name, which requires the -m switch, is issued:

[email protected]  /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.

[email protected]  /c/git/gitub (bogfix)
$ git branch -a
* bugfix
main

Delete renamed Git branch from GitHub

However, if this branch originated from a remote server like GitHub, BitBucket or GitLab, then the incorrectly named branch must be deleted. This can be done using one of the vendor’s online tools, or through the command line:

[email protected]  /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:

[email protected]  /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 a local git branch is renamed, you must push the renamed branch remotely and delete the branch with the old name.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close