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:

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

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

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

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:

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 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
  • How to test a predictive model

    Strategies for testing predictive models and analytics emphasize data quality, real-time testing and code redundancy, as well as ...

  • The dos and don'ts of visual testing

    The visual aspect of an application is an important part of UX. Defects can potentially result in lost sales and damaged ...

  • 3 QA testing tools to consider

    QA testers need to be able to put applications and APIs through their paces. Here are some examples of tools that can help with ...

Cloud Computing