Rename git branch on Local and GitHub Remove using Command


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
Git - Rename Local Branch

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 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'
Git - Renaming Remote GitHub Branch Name
-


Have Questions? Post them here!


Top Hashtags: