How to Rename a Local and Remote Git Branch?

If you're already on the branch (on your local) that you want to rename (along with its corresponding remote branch), then you can do the following:

# rename the branch
git branch -m new-name

# delete the old remote branch and push the new local branch
git push origin :old-name new-name

# switch to the new branch
git checkout new-name

# reset the upstream for the new local branch
git push origin -u new-name

If you're not already on the branch you wish to rename, then you just need to add the local branch name to the first command like so:

git branch -m old-name new-name

Rest of the commands should work in either case.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.