To rename a local Git branch, follow these steps:
1. Switch to the branch you want to rename:
git checkout <old-branch-name>
2. Rename the branch using the git branch command:
git branch -m <new-branch-name>
Alternatively, you can use the longer form of the command:
git branch --move <old-branch-name> <new-branch-name>
3. Push the renamed branch to the remote repository (if you have already pushed the original branch):
git push origin -u <new-branch-name>
The -u option is used to set up tracking for the new branch. If you don’t want to set up tracking, you can omit this option.
4. Delete the old branch on the remote repository (if necessary):
git push origin --delete <old-branch-name>
5. If other team members have cloned the repository, they will need to fetch the changes to see the renamed branch:
git fetch
They can then switch to the renamed branch using:
git checkout <new-branch-name>