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

git branch -m oldname newname

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


当前回答

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

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

其他回答

你不用终端也能做到。您只需要用新名称创建一个分支,然后删除旧名称。

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.

我只是复制和粘贴这个内容从:创建和删除分支

这是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推送github newname:refs/heads/newname 在GitHub网站上,进入设置并将默认分支更改为newname 删除oldname Git推送github——删除旧名

就这么简单。为了在本地和远程重命名一个Git分支,使用这个代码片段(经过测试,效果很好):

git branch -m <oldBranchName> <newBranchName>
git push origin :<oldBranchName>
git push --set-upstream origin <newBranchName>

解释:

重命名步骤:

Git参考: 使用-m或-m选项,<oldbranch>将被重命名为<newbranch>。如果 <oldbranch>有一个对应的reflog,它被重命名为匹配 <newbranch>,并且创建一个reflog条目来记住这个分支 重命名。如果<newbranch>存在,则必须使用-M强制重命名 发生。

删除步骤:

Git参考: 找到一个与experimental in匹配的ref 原始存储库(例如refs/heads/experimental),并删除它。

远程存储库步骤的更新(用于跟踪的上游参考):

Git参考: 对于每个最新或成功推送的分支,添加upstream (tracking)引用,由less参数使用 Git-pull[1]等命令。有关更多信息,请参见 分支。<名称>。在git-config[1]中合并。

以下命令在本地重命名分支,删除远程位置上的旧分支并推送新分支,设置本地分支跟踪新的远程:

git branch -m old_branch new_branch
git push origin :old_branch
git push --set-upstream origin new_branch