我刚刚使用重命名了我的本地分支
git branch -m oldname newname
但这只会重命名分支的本地版本。如何重命名 GitHub 上的那个?
最佳答案
如前所述,在 GitHub 上删除旧的并重新推送,尽管使用的命令比必要的更冗长:
git push origin :name_of_the_old_branch_on_github
git push origin new_name_of_the_branch_that_is_local
稍微剖析一下命令,git push
命令本质上是:
git push <remote> <local_branch>:<remote_branch>
所以在没有指定 local_branch 的情况下进行推送本质上意味着“从我的本地存储库中获取任何内容,并将其设为远程分支”。我一直认为这完全是笨拙的,但它就是这样做的。
截至Git 1.7有一种删除远程分支的替代语法:
git push origin --delete name_of_the_remote_branch
正如@void.pointer 在评论中提到的
Note that you can combine the 2 push operations:
git push origin :old_branch new_branch
This will both delete the old branch and push the new one.
在 ~/.gitconfig
中,这可以变成一个简单的别名,它将远程、原始分支和新分支名称作为参数:
[alias]
branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #"
用法:
git branchm origin old_branch new_branch
请注意,shell 命令中的位置参数在旧版(2.8 之前?)的 Git 中存在问题,因此别名可能会因 Git 版本而异。参见 this discussion了解详情。
关于git - 重命名 GitHub 中的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9524933/