Subrata Sarkar

How to rename a Git branch

Renaming a Git branch is pretty straight forward. I will show you how you can do this both locally and remotely, i.e. at the remote repository level.

Renaming a local branch

  1. Rename current branch
    $ git branch -m <new_name>
  2. Rename any branch while on another branch
    $ git branch -m <old_name> <new_name>
  3. Move to the branch and Rename
    $ git checkout <old_name>
    $ git branch -m <new_name>

Renaming a remote branch

Renaming remote branch means the local branch is already committed. There are two ways you can rename a remote branch. First one might be a bit difficult to remember but the second one is pretty simple and straight forward. If you are a beginner, I would recommend the second option.

Option 1 (assuming that the local branch is already renamed):

  1. Delete the lcoal <old_name> remote branch and push the <new_name> local branch
    $ git push origin :old-name new-name
  2. Reset the upstream branch for the <new-name> local branch
    $ git push origin -u new-name

Option 2 (assuming that the local branch is already renamed):

  1. First push the <new_branch> to remote repository
    $ git push origin -u <new_name>
  2. Now delete the <old_name> from the remote repository
    $ git push origin –delete <old_name>
CAUTION: One thing you need to be careful about while renaming a remote branch. Any Pull Request (PR), that  might already been associated with the old_name branch, will be removed silently and without any prior notice.

Leave a Reply

Your email address will not be published.