To rename a branch in Git, you can use the following commands depending on whether you're renaming the current branch or another branch.
To rename the current branch:
You can use:
git branch -m new-branch-name
To rename a different branch:
If you want to rename a branch that is not currently checked out, use:
git branch -m old-branch-name new-branch-name
Important Notes:
- If you have already pushed the old branch to a remote (like GitHub), you will need to delete the old branch from the remote and push the new branch:
git push origin :old-branch-name # Deletes the old branch from remote
git push origin new-branch-name # Pushes the new branch to remote
- If the old branch exists on the remote, you should also set the upstream for the new branch:
git push --set-upstream origin new-branch-name
Always ensure that you have the necessary permissions and that you coordinate with your team members when renaming branches, especially if they are also working with the same branches.