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

git branch -m oldname newname

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


当前回答

以下命令在本地重命名分支,删除远程位置上的旧分支并推送新分支,设置本地分支跟踪新的远程:

git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch

其他回答

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

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分支,使用这个代码片段(经过测试,效果很好):

git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>

解释:

重命名步骤:

Git参考: 使用-m或-m选项,<oldbranch>将被重命名为<newbranch>。如果 <oldbranch>有一个对应的reflog,它被重命名为匹配 <newbranch>,并且创建一个reflog条目来记住这个分支 重命名。如果<newbranch>存在,则必须使用-M强制重命名 发生。

删除步骤:

Git参考: 找到一个与experimental in匹配的ref 原始存储库(例如refs/heads/experimental),并删除它。

远程存储库步骤的更新(用于跟踪的上游参考):

Git参考: 对于每个最新或成功推送的分支,添加upstream (tracking)引用,由less参数使用 Git-pull[1]等命令。有关更多信息,请参见 分支。<名称>。在git-config[1]中合并。

另一种方法是重命名以下文件:

导航您的项目目录。 将.git/refs/head/[branch-name]重命名为.git/refs/head/new-branch-name。 将.git/refs/remotes/[all-remote-names]/[branch-name]重命名为.git/refs/remotes/[all-remote-names]/new-branch-name。

重命名本地PC和源/远程服务器上的头和远程。

分支现在重命名(本地和远程!)


注意

如果你当前的branch-name包含斜杠(/),Git会像这样创建目录:

当前分支名称:"awe/some/branch"

. /文献/头/敬畏/一些/分支 . /文献/遥控器/ [all-remote-names] /敬畏/一些/分支

祝愿分支-name: "new-branch-name"

导航您的项目目录。 从.git/refs/*/awe/some/中复制分支文件。 把它放在。git/refs/head/中。 从所有的。git/refs/remotes/*/awe/some/中复制分支文件。 把它们放在。git/refs/remotes/*/中。 将所有复制的分支文件重命名为new-branch-name。 检查目录和文件结构现在看起来是否像这样:

. / /头/ new-branch-name。参考文献 . /文献/遥控器/ [all-remote-names] / new-branch-name

对所有远程源/服务器(如果存在的话)执行同样的操作

信息:在远程服务器上通常没有refs/remotes/*目录,因为你已经在远程服务器上;)(好吧,也许在高级Git配置中这是可能的,但我从未见过)

分支现在从awe/some/ Branch重命名为new-branch-name(本地和远程!)

branch-name中的目录被移除。


信息:这种方法可能不是最好的,但它仍然适用于那些可能对其他方法有问题的人

在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

在GitHub端,您可以使用新的(2021年1月)“支持重命名现有分支”(受保护的分支只能由管理员重命名,见末尾)

跟随本教程:https://docs.github.com/en/github/administering-a-repository/renaming-a-branch

参见“如何在GitHub网站上重命名分支?”。

这是一个更好的方法,因为以这种方式重命名分支(在github.com上)将:

Re-target any open pull requests Update any draft releases based on the branch Move any branch protection rules that explicitly reference the old name Update the branch used to build GitHub Pages, if applicable Show a notice to repository contributors, maintainers, and admins on the repository homepage with instructions to update local copies of the repository Show a notice to contributors who git push to the old branch Redirect web requests for the old branch name to the new branch name Return a "Moved Permanently" response in API requests for the old branch name


2021年12月更新:

Restrict renaming protected branches to admins Now, only admins can rename branches that are protected by branch protection rules. GitHub allows repository collaborators to rename every branch in a repository, with the exception of the default branch. When a collaborator renames a branch, any non-wildcard branch protection rules that apply to that branch are also changed to match the branch's new name. Because only admins can modify branch protection rules, renaming of a protected branch is now limited to admin users. For more information, visit Renaming a branch and Managing a branch protection rule.