我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
当前回答
在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.
其他回答
你不用终端也能做到。您只需要用新名称创建一个分支,然后删除旧名称。
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.
我只是复制和粘贴这个内容从:创建和删除分支
在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命令修改名称: Git branch -m feature1 feature2 如果你只是在寻找重命名远程Git分支的命令,这就是它: Git push -u origin feature2:feature3 在执行此操作之前,请检查分支上是否有标记。你可以用git标签做到这一点。
简单三步
Git push origin head Git branch -m old-branch-name new-branch-name Git push origin head
只需删除旧的分支并创建新的分支。
示例(仅重命名远程分支):
git push origin :refs/heads/oldname
git push origin newname:refs/heads/newname
您可能还应该重命名本地分支,并更改推/拉位置的设置。