When you realize that your branch name in Git is not suitable while collaborating on a project, you can rename the branch name easily on your local and remote.
This guide explains how to change the name of the branch on your local and remote.
Rename Any Branch
You can rename any branch using the following command. This command will rename the local branch, then you need to follow the steps mentioned in rename remote branch section to push to remote.
git branch -m old_name new_name
Rename Current Branch
Start by switching to the branch which name needs to be changed in your local.
git checkout old_branch_name
Now you should be switched to the branch name that needs to be renamed.
You can use the
branch command to rename the branch name.
git branch -m new_branch_name
Now you have your local branched renamed to a new one.
Rename Remote branch
To rename the remote branch you need to push the changes to remote and reset the upstream branch.
Use the following command to push local new branch to remote.
git push origin -u new_branch_name
This command will create a new branch with the new name in the remote.
Now you can delete the old branch with the following command.
git push origin --delete old_branch_name
Now you have your branch name renamed in local and remote.
Now you have learned how to change branch name in git local and git remote.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.