Delan Azabani

Renaming GitHub branches

Now that I have a website hosted with GitHub Pages, I've been looking to rename gh-pages branches back to master for some of my repositories so that the root URL namespace isn't automatically polluted.

Unfortunately, some of the solutions I found didn't work because GitHub doesn't allow deleting a repository's default branch. Based on Matthew Brett's Deleting your master branch, a working method for renaming branches is as follows. Start by switching to your old branch, create a new branch based on it, then switch to the new branch:

git checkout $old_branch
git branch $new_branch
git checkout $new_branch

Next, delete your old branch locally:

git branch -D $old_branch

Now, push the new branch up to GitHub:

git push origin $new_branch

Before you can delete the branch on GitHub, go to https://github.com/$user/$repo/settings and change the default branch to $new_branch. Finally, delete the old remote branch:

git push origin :$old_branch

To rename a non-default branch, the process is actually the same, minus changing the default branch on GitHub's repository settings page. If you're using GitHub for Windows, it may fail to load the repository after renaming the default branch; I simply deleted and recloned my local copy, although there is almost certainly a cleaner way.