How to rename git branches

There might be situations when you want to change your git branch
name to something more meaningful or more relative your current context

a couple of cases are below:

  1. Rename branch in git local, when you haven’t pushed it to remote
# renaming the branch when you are currently on the branch
git branch -m new-branch-name

# renaming the branch when you are on a different branch
git branch -m branch-to-be-renamed new-branch-name

  1. Rename branch in git local and remote, when you have pushed it to remote

for this case, we need to follow the step 1 after step 1 we need to delete
the old branch from remote and the final step set the upstream (read about branching) to the new branch.


# renaming the branch when you are currently on the branch
git branch -m new-branch-name

OR 
# renaming the branch when you are on a different branch
git branch -m branch-to-be-renamed new-branch-name

# delete the old branch from remote
git push origin: branch-to-be-renamed                               
# here: is necessary before the old branch name (:branch-to-be-renamed)

# set your upstream to a new branch
git push --set-upstream origin new-branch-name