On one of our longer running projects, we recently ran into a situation where we wanted to rename some branches in git. Specifically, we wanted to rename our master branch to v2-final and rename our v3 branch to master. We looked around for some sort of git rename-branch command, but had no luck and eventually followed these steps:

Note: It is a good idea to take a backup before you start

    1. Rename local branch master to v2-final:
      git branch -m master v2-final
    1. Rename local v3 branch to master:
      git branch -m v3 master
    1. Delete the remote master branch:
      git push origin :master
    1. Push the new master branch (formerly v3) to remote. Note the /refs/heads/ prefix is needed when the remote branch does not exist (because we deleted it in step 3)
      git push origin master:refs/heads/master
    1. Push the new v2-final branch (formerly master) to remote
      git push origin v2-final:refs/heads/v2-final
    1. Delete the old v3 branch from remote
      git push origin :v3
    1. At this point, the renamed local master branch will still be trying to track the remote v3 branch and you will see a warning on git status of “Your branch is based on ‘origin/v3’, but the upstream is gone.” To fix this:
      git checkout master
      git branch --unset-upstream
      git branch --set-upstream-to=origin/master master
  1. The same applies to the new v2-final branch which will still be tracking the remote master branch producing a warning like: “Your branch and ‘origin/master’ have diverged, and have XXX and YYY different commits each, respectively.”
    Fix this in the same manner as step 7 and you’re done!
    git checkout v2-final
    git branch --unset-upstream
    git branch --set-upstream-to=origin/v2-final v2-final