How To Rename (Local and Remote) Git Branch?

Git provides branches in order to create multiple copies of the current code repository. Branches can be identified by their hashes and names but the names are more human-friendly for identification. Branche names can be set at the start of the branch creation but also changes after creation. In this tutorial, we examine how to rename or change the current name of the branch. We explain both local and remote branch renaming.

Rename Local Git Branch

The local branches are located in the current computer and can not be accessed by other computers. Local branches can be renamed by using the git branch command by providing the -m option with the branch current name and a new name. The syntax of the renaming local git branch is like below.

git branch -m CURRENT_NAME NEW_NAME
  • CURRENT_NAME is the local branch current or old name.
  • NEW_NAME is the new name of the branch.

In the following example, we change the name of the local git branch “testing” to the “preprod”.

$ git branch -m testing preprod

Rename Currently Active Local Branch

We can change the currently active local branch without providing its name and just providing the new name. In the following example, we change the name of the currently active local branch into the “preprod”.

$ git branch -m "preprod"

Rename Remote Git Branch

Remote Git repositories may contain branches. We can rename the remote git branches by using the git push origin command. But this can be completed with some steps. First, we delete the old remote branch like below.

$ git push origin :testing

Then we reset the upstream branch for the new name.

$ git push -u preprod

Leave a Comment