在github中重命名一个分支 [英] Renaming a branch in github

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

问题描述

我刚刚使用

  git branch -m oldname newname 

但这只会重命名分支的本地版本。我如何重命名 github 中的一个?


解决方案

&安培;重新推送,尽管使用的命令比必要的冗长一些:

  git push origin:name_of_the_old_branch_on_github 
git推送原点new_name_of_the_branch_that_is_local

简单。 git push命令本质上是:

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

因此,在没有指定local_branch的情况下执行push本质上意味着从我的本地存储库中不采取任何措施,远程分支。我一直认为这是完全错误的,但它是完成的方式。 编辑 Git 1.7 有一个替代语法用于删除远程分支:

  git push origin --delete name_of_the_remote_branch 

编辑:正如@ void.pointer在评论中提到的那样


请注意,您可以合并2个推送操作:

git push origin:old_branch new_branch



这将删除旧分支并推送新分支。


这可以转化为〜/ .gitconfig 中的一个简单别名,它将远程原始分支和新分支名称作为参数:

  [别名] 
branchm =!git分支-m $ 2 $ 3&&am p; git push $ 1:$ 2 $ 3 -u#

用法:

  git branchm origin old_branch new_branch 

请注意,shell命令中的位置参数在git的旧版本(2.8之前的版本)中存在问题,因此别名可能会因git版本而异。详情请参阅这个讨论


I just renamed my local branch using

git branch -m oldname newname

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

解决方案

As mentioned, delete the old one on Github & 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

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

git push <remote> <local_branch>:<remote_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.

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

git push origin --delete name_of_the_remote_branch

Edit: As mentioned by @void.pointer in the comments

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.

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 #"

Usage:

git branchm origin old_branch new_branch

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天全站免登陆