To rename a branch in Git
, you can use the following steps:
1. Check the current branch:
Before renaming the branch, ensure that you are not currently on the branch you want to rename. If you are on the branch, you can switch to a different branch using the `git checkout`
2. Rename the branch:
git checkout <other-branch>
To rename a branch, you can use the `git branch`
command with the `-m`
option followed by the current branch name and the new desired name.
git branch -m <current-branch-name> <new-branch-name>
For example, if you want to rename the branch named "feature-branch"
you would use the following command:
git branch -m feature-branch new-feature-branch
Note that if you are renaming the branch you are currently on, you need to switch to a different branch first.
3. Push the renamed branch (optional):
If the branch you renamed was already pushed to a remote repository, the remote repository will still have the old branch name. To update the remote repository with the new branch name, you can use the `git push`
command with the `--set-upstream`
git push -u origin <new-branch-name>
Replace `<new-branch-name>` with the new name of the branch you just renamed.
This command sets the upstream branch and pushes the changes to the remote repository. Subsequent pushes or pulls can be done using the new branch name.
4. Delete the old remote branch (optional):
If you have renamed the branch and pushed the changes to the remote repository, you might want to delete the old branch with the previous name from the remote repository. You can use the `git push`
command with the `--delete`
option followed by the old branch name to delete it from the remote repository.
git push origin --delete <old-branch-name>
Replace `<old-branch-name>` with the previous name of the branch you renamed.
By following these steps, you can easily rename a branch in Git, locally and optionally update the remote repository with the new branch name. Remember to update your local repositories and inform your collaborators if you have renamed a branch that was already pushed to a remote repository to ensure a smooth workflow.