How to rename a branch? | The Git Branch Command

It is possible that you named a branch wrongly. You might want to rename a local branch or a branch which you already pushed to remote.
Rename a branch which you only have locally
In case that you are on the branch to be renamed, then use the branch command as follows:
In case that you are on another branch, then use the branch command as follows:
Rename a branch which you already have pushed to remote
Rename the branch as mentioned before depending on whether you are on the branch by using the branch command as follows:
or
You should now checkout the renamed branch if you didn't do this already. Then push the branch with its new name to remote by using the push command:
By using the -u
flag, you set the branch to track the upstream branch with the new name, i.e. origin/new-name
. Otherwise, the renamed branch will continue to track origin/old-name
.
Now you can delete the old remote branch, i.e. origin/old-name
by using the git push command as follows:
Reference
For full reference see Git branch documentation and Git push documentation.