How to rename a local Git branch? To rename a local Git branch, you can use the following command:
bashgit branch -m <old-name> <new-name>
Here, <old-name> represents the current branch name, and <new-name> is the new branch name you intend to use.
For example, if your branch is currently named feature-x and you want to rename it to feature-y, you can execute:
bashgit branch -m feature-x feature-y
Before renaming the branch, ensure you have checked out the branch you wish to rename:
bashgit checkout feature-x git branch -m feature-x feature-y
If you attempt to rename a branch that is not currently checked out, use the following command:
bashgit branch -m <old-name> <new-name>
If you have already pushed the old branch to the remote repository and also want to rename the remote branch, first delete the old remote branch, then push the new branch name, and reset the upstream branch:
- Delete the old remote branch:
bashgit push origin --delete <old-name>
- Push the new branch to the remote and set the upstream branch:
bashgit push origin -u <new-name>
Please note that before pushing the new branch name to the remote repository, verify that no other team members are actively using the old branch, as this change will affect all users of the branch. It is advisable to communicate with your team in advance to ensure everyone is aware of the branch name update.