Created: 2021-07-28

How to rename local and remote git branches

We all encounter the situation of having to rename a git branch sooner or later. In this article I'll describe one way that worked for me.

Rename the local branch

There are two ways to rename the local branch

  1. Activate the branch you want to rename first by firing up git checkout <branchname> and then git branch -m <new-branchname>
  2. From another branch by firing up git branch -m <old-branchname> <new-branchname>

Rename the remote branch

If you have renamed the local branch as described above you can delete the old and push the new one by

git push origin --delete <old-branchname>

and it can happen that you get error messages from the remote similar to the following:

remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To server:projects/repo.git
 ! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'server:projects/repo.git'

If this happens ssh to your remote cd into the repository's directory and set a new symbolic ref by

git symbolic-ref HEAD refs/heads/<new-branchname>

and now repeat the

git push origin --delete <old-branchname>

command should work and finally set the upstream branch of your local repository as well

git push origin -u <new-branchname>