in Coding, DevOps, git, Uncategorized

How to Rename a local and remote branch in git

Just in case you pushed a wrongly named branch. wink

1. Rename your local branch.
Get to the branch you want to rename:

git branch -m new-name

2. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:

git push origin -u new-name

Note: Make sure you run command git status and check that newly created branch is pointing to its own ref and not older one. If you find the reference to older branch, you need to unset the upstream using

git branch --unset-upstream