In order to rename a git branch locally make use of the git branch command with -m or --move option.
Renaming Local Git Branch
-
Command Syntax:
% git branch -m old-branch-name new-branch-name
% git branch --move old-branch-name new-branch-name
Examples:
% git branch -m release/ticket01 release/ticket-1
% git branch -m master main

Renaming Remote GitHub Branch
In order to rename a git branch on the remote git server like GitHub, make use of the git push command with the following syntax,
Command Syntax:
% git push origin :old-branch-name new-branch-name
Examples:
% git branch --remote
origin/HEAD -> origin/main
origin/main
origin/release01
As you can see I have three branches at the remote repo and I want to rename release01 to release-1,
% git push origin :release01 release-1
Username for 'https://github.com': gituser
Password for 'https://gituser@github.com':
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/gituser/myrepo.git
- [deleted] release01
* [new branch] release-1 -> release-1
Note: Make sure that you have the branch renamed on your local repo before trying this or else you will get an error,
% git push origin :release01 release-11
error: src refspec release-11 does not match any
error: failed to push some refs to 'https://github.com/gituser/myrepo.git'

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Ventura 13.
Please support independent contributors like Code2care by donating a coffee.
Buy me a coffee!

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!