Skip to content

Instantly share code, notes, and snippets.

@chuckntaylor
Last active July 19, 2024 13:48
Show Gist options
  • Save chuckntaylor/6fef7aebc22350c7974abf08e344a61d to your computer and use it in GitHub Desktop.
Save chuckntaylor/6fef7aebc22350c7974abf08e344a61d to your computer and use it in GitHub Desktop.
Git: Rename local and remote branch

Renaming both a local and remote branch

First, rename the local branch by typing:

git branch -m <old_name> <new_name>

Then push the new branch to the remote repository:

git push origin <new_name>

Delete the old branch on the remote repository: ** NOTE: With GitHub, you can't delete a branch through the web interface if it's the default branch of the repository. You can change the default branch in the repository settings. **

git push origin :<old_name>

Finally, reset the upstream branch for the new_name local branch:

git push origin -u <new_name>

Renaming a remote branch

If you only want to rename a remote branch, you can do so by:

git push origin <old_name>:<new_name>
git push origin :<old_name>

Finally, reset the upstream branch for the new_name local branch:

git push origin -u <new_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment