How To Rename A Remote Branch in Git

In order to rename the remote branch we need to rename it locally, to remove the old name of branch from remote repository and then to push the branch renamed with the new name to the remote repository:

  1. Switch to the branch you want to rename, then rename it it locally:

    git checkout old_branch
    git branch -m old_branch new_branch
  2. Remove old branch from remote repository:

    git push origin :old_branch
  3. Push the renamed local branch to remote repository:

    git push origin new_branch

Full Console:

$git checkout old_branch 
    Switched to branch 'old_branch'

$git branch
* old_branch

$git branch -m old_branch new_branch

$git branch
* new_branch

$git push origin :old_branch 
Password for 'https://[email protected]':
To https://[email protected]/user/repository.git
 - [deleted]         old_branch 

$git push origin new_branch
Password for 'https://[email protected]':
Counting objects: 1, done.
Writing objects: 100% (1/1), 187 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://[email protected]/user/repository.git
 * [new branch]      new_branch -> new_branch

Leave a Reply

Your email address will not be published. Required fields are marked *