git - 重命名 GitHub 中的分支

标签 git github branch rename

我刚刚使用重命名了我的本地分支

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/

相关文章:

git - 我如何发布到 <me>.github.com?

git - 在 Gerrit 代码审查中更改消息

android - AOSP 存储库与私有(private)存储库同步

git - 如何使现有的 git 分支跟踪远程 SVN 分支?

svn:合并工作流程的细微差别

git - 无法从 Sourcetree pull Git 远程存储库

git - Jenkins 在Docker错误:CP命令无法找到目录

amazon-web-services - 来自 github 的模板路径无效,但直接在 cli Cloudformation 中执行时无效

c - 将 0 映射到任何非零值同时保留其他值的无分支方式?

ruby-on-rails - 如何还原已 checkin 到 git 的 Rails 迁移