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

git branch -m oldname newname

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


当前回答

在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 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——这就违背了我的意图。

这篇文章展示了如何做到这一点非常简单。

要重命名本地Git分支,我们可以使用Git branch -m命令修改名称: Git branch -m feature1 feature2 如果你只是在寻找重命名远程Git分支的命令,这就是它: Git push -u origin feature2:feature3 在执行此操作之前,请检查分支上是否有标记。你可以用git标签做到这一点。

如前所述,在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版本而有所不同。有关详细信息,请参阅此讨论。

在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 push origin :refs/heads/oldname
git push origin newname:refs/heads/newname

您可能还应该重命名本地分支,并更改推/拉位置的设置。