我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
当前回答
分支重命名现在可以通过GitHub API使用
你可以用GitHub REST API重命名一个分支。
你可以通过gh CLI轻松运行API命令,就像这样:
gh api "repos/{owner}/{repo}/branches/{branch}/rename" -f new_name={newBranch}
其他回答
这篇文章展示了如何做到这一点非常简单。
要重命名本地Git分支,我们可以使用Git branch -m命令修改名称: Git branch -m feature1 feature2 如果你只是在寻找重命名远程Git分支的命令,这就是它: Git push -u origin feature2:feature3 在执行此操作之前,请检查分支上是否有标记。你可以用git标签做到这一点。
这是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 push origin head Git branch -m old-branch-name new-branch-name Git push origin head
我找到了三个关于如何更改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本地和远程分支
只需删除旧的分支并创建新的分支。
示例(仅重命名远程分支):
git push origin :refs/heads/oldname
git push origin newname:refs/heads/newname
您可能还应该重命名本地分支,并更改推/拉位置的设置。