How to rename a remote git branch name

I have 4 braches like master -> origin/regacy, FeatureA -> origin/FeatureA.

As you can see above, I typed a wrong name.

So I want to rename a remote branch name ( origin/regacy => origin/legacy or origin/master)

I try to command below.

git remote rename regacy legacy

But git console retured error msg to me.

error : Could not rename config section 'remote.regacy' to 'remote.legacy'

How can i solve this problem?


You can't directly rename a remote branch.
You have to delete it and then re-push it.

Renaming a branch

# rename the local branch to the new name
git branch -m <old_name> <new_name> 

# delete the old branch on remote - where <remote> is eg. origin
git push <remote> --delete old_name

# push the new branch to remote         
git push <remote> new_name

在这里输入图像描述


Important note :

When you use the git branch -m (move), git is also updating your tracking branch with the new name.

git remote rename regacy legacy

git remote rename is trying to update your remote section in your config file. It will rename the remote with the given name to the new name, but in your case it did not find any, so the renaming failed.

But it will not do what you think, it will rename your local config remote name and not the remote branch.


Note Git servers might allow you to rename git branch using the web interface or external programs (like Sourctree etc) but you have to keep in mind that in git all the work is done locally so its recommended to use the above commands to the work.


If you have named a branch incorrectly AND pushed this to the remote repository follow these steps to rename that branch (based on this article):

  • Rename your local branch:

  • If you are on the branch you want to rename:
    git branch -m new-name

  • If you are on a different branch:
    git branch -m old-name new-name

  • Delete the old-name remote branch and push the new-name local branch :
    git push origin :old-name new-name

  • Reset the upstream branch for the new-name local branch :
    Switch to the branch and then:
    git push origin -u new-name


  • It seems that there is a direct way:

    If you really just want to rename branches remotely (without renaming any local branches at the same time) you can do this with a single command like

    git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

    https://stackoverflow.com/a/21302474/2586761

    See the original answer for more detail

    链接地址: http://www.djcxy.com/p/2610.html

    上一篇: 如何将我的git'master'分支重命名为'release'?

    下一篇: 如何重命名远程git分支名称