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

git branch -m oldname newname

但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?


当前回答

在Git分支中,运行:

git branch -m old_name  new_name

这将修改本地存储库中的分支名称:

git push origin :old_name new_name

这将把修改后的名称推到远程并删除旧的分支:

git push origin -u new_name

它设置本地分支来跟踪远程分支。

这就解决了问题。

其他回答

你不用终端也能做到。您只需要用新名称创建一个分支,然后删除旧名称。

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 push origin head Git branch -m old-branch-name new-branch-name Git push origin head

下载Atlassian Sourcetree(免费)。 导入存储库的本地克隆。 在侧边栏中右键单击要重命名的分支。从上下文菜单中选择“Rename branch…”,并重命名它。 推到原点。

这是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分支名称的命令,这些命令是一种更快的方法:

git branch -m old_branch new_branch         # Rename branch locally
git push origin :old_branch                 # Delete the old branch
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

如果你需要循序渐进,你可以阅读这篇很棒的文章:

如何重命名Git本地和远程分支