git: renaming branches remotely?

如果有一个仓库,我只有git://访问(通常只需push + pull),有没有一种方法可以像在本地使用git branch -m一样对存储库中的分支进行重命名?


You just have to create a new local branch with the desired name, push it to your remote, and then delete the old remote branch:

$ git branch new-branch-name origin/old-branch-name
$ git push origin --set-upstream new-branch-name
$ git push origin :old-branch-name

Then, to see the old branch name, each client of the repository would have to do:

$ git fetch origin
$ git remote prune origin

NOTE: If your old branch is your main branch, you should change your main branch settings. Otherwise, when you run $ git push origin :old-branch-name , you'll get the error "deletion of the current branch prohibited".


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:

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

I wrote this script (git-rename-remote-branch) which provides a handy shortcut to do the above easily.

To integrate @ksrb's comment: What this basically does is two pushes in a single command, first git push <remote> <remote>/<old_name>:refs/heads/<new_name> to push a new remote branch based on the old remote tracking branch and then git push <remote> :<old_name> to delete the old remote branch.


First checkout to the branch which you want to rename

git branch -m old_branch new_branch
git push -u origin new_branch

To remove old branch from remote:

git push origin :old_branch
链接地址: http://www.djcxy.com/p/2606.html

上一篇: 在Git仓库中更改分支名称

下一篇: git:重命名远程分支?