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

git branch -m oldname newname

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


当前回答

下面的命令对我有用:

git push origin :old-name-of-branch-on-github
git branch -m old-name-of-branch-on-github new-name-for-branch-you-want
git push origin new-name-for-branch-you-want

其他回答

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

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

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

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

简单三步

Git push origin head Git branch -m old-branch-name new-branch-name Git push origin head

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

要重命名本地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 branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch