Renaming a branch in GitHub

Question:

I just renamed my local branch using

but this only renames the local version of the branch. How can I rename the one on GitHub?

Answer:

Delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:

Dissecting the commands a bit, the git push command is essentially:

So doing a push with no local_branch specified essentially means “take nothing from my local repository, and make it the remote branch”. I’ve always thought this to be completely kludgy, but it’s the way it’s done.

As of Git 1.7 there is an alternate syntax for deleting a remote branch:

Note: that you can combine the 2 push operations:

git push origin :old_branch new_branch

This will both delete the old branch and push the new one.

This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig:

Usage:

Note: that positional arguments in shell commands was problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version.

Leave a Reply