In order to rename a git branch locally make use of the git branch command with -m or --move option.
Renaming Local Git Branch
% git branch -m old-branch-name new-branch-name
% git branch --move old-branch-name new-branch-name
% 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,
% git push origin :old-branch-name new-branch-name
% 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://email@example.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!
Facing issues? Have Questions? Post them here! I am happy to answer!
- Fix [oh-my-zsh] Cant update: not a git repository
- [fix] fatal: pathspec index.html did not match any files error
- How to ignore files in git using .gitignore file
- Git Fix: fatal: refusing to merge unrelated histories Error
- Install GitHub Command Line Tool on Mac
- How to change directory in Git bash
- Get List of all local branches git command
- fix fatal: --local can only be used inside a git repository error
- [git] fatal: your current branch 'main' does not have any commits yet
- Git Revision Questions Before the Interview
- Change the default git branch name from master to main
- git command to remove/unstage files from staging area
- Remove git config at Local, Global or System Levels?
- [fix] fatal: this operation must be run in a work tree in git
- Clone a particular remote brach using git clone command
- How do I get a list of all branches in Git?
- How to check your installed version of Git
- Step-by-Step: How to delete a git branch from local as well as remote origin
- Git Commit - Author identity unknown, Please tell me who you are email
- 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 Rename a Git Local Branch Using the --move Option
- Perform an Empty Commit in Git without anything in Staging Area
- [Fixed] Git Clone firstname.lastname@example.org: Permission denied (publickey) Could not read from remote repository Fatal error
- GitHub: How to Search Code
- Rename git branch on Local and GitHub Remove using Command
- Open Current Directory in Finder using Mac Terminal - MacOS
- How to check if Java main thread is alive - Java
- SharePoint list excel import error - Title is a required filed and can't be empty - SharePoint
- Resolving DNS_PROBE_FINISHED_NXDOMAIN Google Chrome Error - Google
- Fix AWS CLI 2 SignatureDoesNotMatch Error - Signature we calculated does not match - AWS
- How to install xz data compression software using Brew - HowTos
- How to Show Line Numbers in Jupyter Notebook Cells - Python
- Convert Float to String in Python - Python