Patricia Sauer - Authentic Programming

How to rename a branch? | The Git Branch Command

A change neon light

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:

git branch -m new-name

In case that you are on another branch, then use the branch command as follows:

git branch -m old-name new-name

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:

git branch -m new-name

or

git branch -m old-name new-name

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:

git push -u origin new-name

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:

git push origin :old-name

Reference

For full reference see Git branch documentation and Git push documentation.

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer