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 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'

-
Have Questions? Post them here!
More Posts related to Git,
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- Github: How to Invite Collaborators
- Git Commit - Author identity unknown, Please tell me who you are email
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- Github: fatal: Authentication failed Support for password was removed on August 13, 2021
- Get List of all local branches git command
- Git Revision Questions Before the Interview
- git fatal: Authentication failed error [fix]
- How to Install Git on Ubuntu Linux
- GitHub: How to Search Code
- How to change Git Default Author and Committer details in Eclipse
- How to check your installed version of Git
- Install GitHub Command Line Tool on Mac
- How to Merge Branch into Master Branch
- Perform an Empty Commit in Git without anything in Staging Area
- Git Fix: fatal: refusing to merge unrelated histories Error
- How to undo last Git Commit on Local Repository?
- git command to remove/unstage files from staging area
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial
- Remove git config at Local, Global or System Levels?
- Fix [oh-my-zsh] Cant update: not a git repository
- Git Config Command - A Deep Dive
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to change directory in Git bash
More Posts:
- Turn on off volume change button sounds Mac OS X - Mac-OS-X
- Word-wrap Eclipse Console logs - Eclipse
- Delete Android Studio Projects - Android-Studio
- Eclipse Error : The Eclipse executable launcher was unable to locate its companion shared library. - Eclipse
- How to clear ZSH history of commands executed on Mac Terminal - zsh
- Install Native Java JDK JRE on Apple Silicon M1 Mac - MacOS
- [fix] Cannot connect to the Docker daemon at unix:var/run/docker.sock. Is the docker daemon running? - Docker
- Multiple ways to Convert tabs with spaces in Notepad++ - NotepadPlusPlus