I want to rename a Git branch but I’m confused about the correct way to do it, especially if the branch already exists on GitHub. What commands should I use to rename it locally and remotely without causing issues?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re currently on the branch you want to rename, use:
“`bash
git branch -m new-branch-name
“`
If you’re renaming a different branch:
“`bash
git branch -m old-branch-name new-branch-name
“`
If the branch has already been pushed to GitHub, you’ll also need to update the remote:
“`bash
git push origin -u new-branch-name
git push origin –delete old-branch-name
“`
After that, update any open pull requests or references that still point to the old branch name. That’s usually all that’s needed to rename a branch both locally and on GitHub.