Skip to content

Instantly share code, notes, and snippets.

@niqdev
Last active January 15, 2020 18:17
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niqdev/a327c3d8d24ddbd7584c to your computer and use it in GitHub Desktop.
Save niqdev/a327c3d8d24ddbd7584c to your computer and use it in GitHub Desktop.
Rename local and remote branch on GitHub
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
@tukusejssirs
Copy link

tukusejssirs commented Feb 4, 2018

At first, this didn’t help me, because I could’t delete my master branch with git push origin :master (error: failed to push some refs to 'git@github.com:[user]/[repo].git'). However, I’ve found a website, where I found following commands, which are working.

git branch -m old_branch new_branch         # Rename branch locally
git checkout new_branch                     # If not on old branch already
git push origin placeholder                 # Push up the placeholder branch
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

# Then set the new branch to be the github default branch.
# Go to the main github page for your forked repository, and click on the ‘Admin’ button.
git push origin :old_branch                 # Delete the old branch

Edit

For some reason, afterwards I needed to do the following two commands, too:

git branch --unset-upstream                 # Unset the old branch as origin
git push --set-upstream origin new_branch   # Set the new branch as origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment