How to Rename a Git branch
Renaming a Git branch is straightforward. Follow these steps depending on whether you're working with a local or remote branch.
Rename a Local Git Branch
Step 1: Switch to the Branch You Want to Rename
You can't rename the branch you're currently on. Switch to a different branch first:
Step 2: Rename the Branch
Use the following command to rename the branch:
For example:
Rename a Remote Git Branch
Renaming a remote branch requires additional steps:
Step 1: Rename the Local Branch
Follow the steps above to rename the local branch.
Step 2: Delete the Old Remote Branch
Push the deletion of the old branch to the remote:
Example:
Step 3: Push the Renamed Branch to Remote
Push the new branch to the remote repository:
Step 4: Update Remote Tracking (Optional)
If your new branch should track the remote branch, set the upstream: