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

git branch -m oldname newname

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


当前回答

就我而言,我需要一个额外的命令,

git branch --unset-upstream

让我的重命名分支推到原点newname。

(为了便于输入),我首先git签出oldname。 然后执行如下命令:

Git branch -m newname <br/> Git push origin:oldname*或* Git push origin——delete oldname Git分支——unset-upstream Git push -u origin newname或Git push origin newname

这个额外的步骤可能只是必要的,因为我(倾向于)通过git push -u origin oldname在我的分支上设置远程跟踪。这样,当我签出oldname时,我随后只需要键入git push而不是git push origin oldname。

如果我没有在git push origin newbranch之前使用命令git branch——unset-upstream, git会重新创建oldbranch并将newbranch推到原点oldbranch——这就违背了我的意图。

其他回答

在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.

就这么简单。为了在本地和远程重命名一个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]中合并。

分支重命名现在可以通过GitHub API使用

你可以用GitHub REST API重命名一个分支。

你可以通过gh CLI轻松运行API命令,就像这样:

gh api "repos/{owner}/{repo}/branches/{branch}/rename" -f new_name={newBranch}

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

在Git分支中,运行:

git branch -m old_name  new_name

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

git push origin :old_name new_name

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

git push origin -u new_name

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

这就解决了问题。