我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
我刚刚重命名了我的本地分支使用
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
其他回答
下载Atlassian Sourcetree(免费)。 导入存储库的本地克隆。 在侧边栏中右键单击要重命名的分支。从上下文菜单中选择“Rename branch…”,并重命名它。 推到原点。
这是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分支,我们可以使用Git branch -m命令修改名称: Git branch -m feature1 feature2 如果你只是在寻找重命名远程Git分支的命令,这就是它: Git push -u origin feature2:feature3 在执行此操作之前,请检查分支上是否有标记。你可以用git标签做到这一点。
以下命令在本地重命名分支,删除远程位置上的旧分支并推送新分支,设置本地分支跟踪新的远程:
git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch
分支重命名现在可以通过GitHub API使用
你可以用GitHub REST API重命名一个分支。
你可以通过gh CLI轻松运行API命令,就像这样:
gh api "repos/{owner}/{repo}/branches/{branch}/rename" -f new_name={newBranch}