重命名 GitHub 中的分支 [英] Renaming a branch in GitHub

查看:40
本文介绍了重命名 GitHub 中的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用

git branch -m oldname newname

但这只会重命名分支的本地版本.我如何重命名 GitHub 上的那个?

but this only renames the local version of the branch. How can I rename the one on GitHub?

推荐答案

如前所述,在 GitHub 上删除旧的并重新推送,尽管使用的命令比必要的要冗长一些:

As mentioned, delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:

git push origin :name_of_the_old_branch_on_github
git push origin new_name_of_the_branch_that_is_local

稍微剖析一下命令,git push 命令本质上是:

Dissecting the commands a bit, the git push command is essentially:

git push <remote> <local_branch>:<remote_branch>

因此,在没有指定 local_branch 的情况下进行推送基本上意味着从我的本地存储库中不取任何东西,并将其设为远程分支".我一直认为这很笨拙,但它就是这样做的.

So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.

Git 1.7 开始,有删除远程分支的替代语法:

As of Git 1.7 there is an alternate syntax for deleting a remote branch:

git push origin --delete name_of_the_remote_branch

正如@void.pointer 在评论中提到的

As mentioned by @void.pointer in the comments

请注意,您可以结合使用 2 个推送操作:

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中以远程、原始分支和新分支名称作为参数:

This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.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 版本而异.有关详细信息,请参阅此讨论.

Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version. See this discussion for details.

这篇关于重命名 GitHub 中的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆