
There are a few ways to accomplish that:
- Change your local branch and then push your changes
- Push the branch to remote with the new name while keeping the original name locally
Renaming local and remote
# Rename the local branch to the new name
git branch -m <old_name> <new_name>
# Delete the old branch on remote - where <remote> is, for example, origin
git push <remote> --delete <old_name>
# Or shorter way to delete remote branch [:]
git push <remote> :<old_name>
# Prevent git from using the old name when pushing in the next step.
# Otherwise, git will use the old upstream name instead of <new_name>.
git branch --unset-upstream <new_name>
# Push the new branch to remote
git push <remote> <new_name>
# Reset the upstream branch for the new_name local branch
git push <remote> -u <new_name>

Renaming Only remote branch
Credit: ptim
# In this option, we will push the branch to the remote with the new name
# While keeping the local name as is
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
Important note:
When you use the git branch -m
(move), Git is also updating your tracking branch with the new name.
git remote rename legacy legacy
git remote rename
is trying to update your remote section in your configuration file. It will rename the remote with the given name to the new name, but in your case, it did not find any, so the renaming failed.
But it will not do what you think; it will rename your local configuration remote name and not the remote branch.
Note
Git servers might allow you to rename Git branches using the web interface or external programs (like Sourcetree, etc.), but you have to keep in mind that in Git all the work is done locally, so it's recommended to use the above commands to the work.
git branch -M new-name
3.git push -d origin old-name
4.git push -u origin new-name
.old name
2. Renamegit branch –m old-name new-name
3. Checkout into new branchgit checkout new name
4. Push changes to new remotegit push -u origin new-name
5. Go to the web page create PR in GH, you will see the new branch as well as the old branch 6. Back to branchnew name
you can now delete the origin head of the old branchgit push -d origin old-name
the local and remote now will have only one branch with all the commits in the new branch. The old branch has been safely deleted AFTER new branch created.