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'
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Git,
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
More Posts:
- Center Align TextView Android Horizontally or Vertically - Android
- Block Select Text in TextEdit on Mac - MacOS
- Advanced ways to set Custom Settings for a Website on Safari for Mac - MacOS
- How to change Android Titlebar theme color from Purple - Android
- Implementing Android Text to Speech Example - Android
- cURL HTTP GET request command examples - HowTos
- Fix: Xbox Error Code: 0x80190190 - Microsoft
- How to open a file via Mac Terminal - MacOS