我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
当前回答
你不用终端也能做到。您只需要用新名称创建一个分支,然后删除旧名称。
Create a branch In your repository’s branch selector, just start typing a new branch name. It’ll give you the option to create a new branch: It’ll branch off of your current context. For example, if you’re on the bugfix branch, it’ll create a new branch from bugfix instead of master. Looking at a commit or a tag instead? It’ll branch your code from that specific revision. Delete a branch You’ll also see a delete button in your repository’s Branches page: As an added bonus, it’ll also give you a link to the branch’s Pull Request, if it has one.
我只是复制和粘贴这个内容从:创建和删除分支
其他回答
在Git本地和远程中重命名分支
1. 重命名您的本地分支。
如果你在你想重命名的分支上:
git branch -m new-name
如果你在不同的分支上:
git branch -m old-name new-name
2. 删除旧名称的远程分支,并推送新名称的本地分支。
git push origin :old-name new-name
3.重置新名称本地分支的上游分支。
切换到分支,然后:
git push origin -u new-name
所以结论是:
git branch -m new-name
git push origin :old-name new-name
git push origin -u new-name
下面的命令对我有用:
git push origin :old-name-of-branch-on-github
git branch -m old-name-of-branch-on-github new-name-for-branch-you-want
git push origin new-name-for-branch-you-want
这是Hazarapet Tunanyan回答的附加条件。
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
# You might be getting an error doing the above step, skip to the next step
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
你在git push origin:old_branch时会得到一个错误,因为你试图删除的old_branch可能是默认的分支。
只需要执行另外2步,然后转到github,从设置中更改默认分支,然后你就可以执行git push origin:old_branch了。
简单三步
Git push origin head Git branch -m old-branch-name new-branch-name Git push origin head
如前所述,在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提到的。注释中的指针
注意,你可以结合这两个push操作: Git推送源:old_branch new_branch 这将删除旧的分支并推送新的分支。
这可以转换为一个简单的别名,将远程的、原始的分支和新的分支名作为参数,在~/.gitconfig中:
[alias]
branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #"
用法:
git branchm origin old_branch new_branch
注意,shell命令中的位置参数在较旧的Git版本(2.8之前?)中是有问题的,因此别名可能会根据Git版本而有所不同。有关详细信息,请参阅此讨论。