Sometimes we need to rename an existing Git branch without creating a new branch or removing the old branch.
First, make sure you have the existing branch to rename checked out:
$ git branch
Output:
main * old-name
To rename the branch use:
$ git branch -m old-name new-name
The command ‘m’ is short for “move”, similar to moving a file to rename it in Unix systems.
Confirm the new name:
$ git branch
Output:
main * new-name