How to rename a branch you already pushed to remote?
Let’s say you start working on a task and create a branch with the same name. But as you progress further, you realise that the work itself doesn’t describe the task name or the nature of work changes in between. However, you have already pushed your changes on remote.
How do you rename the branch both locally and remotely?
- git checkout <old_name>
- git branch -m <new_name>
- git push origin -u <new_name>
- git push origin — delete <old_name>
There are also different ways of achieving the same outcome. For example, this gist does it in fewer lines.
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
There are also GUI tools that help you do the same in a few clicks.