我刚刚重命名了我的本地分支使用
git branch -m oldname newname
但这只是重命名分支的本地版本。我如何在GitHub上重命名一个?
我刚刚重命名了我的本地分支使用
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
其他回答
另一种方法是重命名以下文件:
导航您的项目目录。 将.git/refs/head/[branch-name]重命名为.git/refs/head/new-branch-name。 将.git/refs/remotes/[all-remote-names]/[branch-name]重命名为.git/refs/remotes/[all-remote-names]/new-branch-name。
重命名本地PC和源/远程服务器上的头和远程。
分支现在重命名(本地和远程!)
注意
如果你当前的branch-name包含斜杠(/),Git会像这样创建目录:
当前分支名称:"awe/some/branch"
. /文献/头/敬畏/一些/分支 . /文献/遥控器/ [all-remote-names] /敬畏/一些/分支
祝愿分支-name: "new-branch-name"
导航您的项目目录。 从.git/refs/*/awe/some/中复制分支文件。 把它放在。git/refs/head/中。 从所有的。git/refs/remotes/*/awe/some/中复制分支文件。 把它们放在。git/refs/remotes/*/中。 将所有复制的分支文件重命名为new-branch-name。 检查目录和文件结构现在看起来是否像这样:
. / /头/ new-branch-name。参考文献 . /文献/遥控器/ [all-remote-names] / new-branch-name
对所有远程源/服务器(如果存在的话)执行同样的操作
信息:在远程服务器上通常没有refs/remotes/*目录,因为你已经在远程服务器上;)(好吧,也许在高级Git配置中这是可能的,但我从未见过)
分支现在从awe/some/ Branch重命名为new-branch-name(本地和远程!)
branch-name中的目录被移除。
信息:这种方法可能不是最好的,但它仍然适用于那些可能对其他方法有问题的人
以下是对我有效的方法:
首先创建新分支: Git推送github newname:refs/heads/newname 在GitHub网站上,进入设置并将默认分支更改为newname 删除oldname Git推送github——删除旧名
这篇文章展示了如何做到这一点非常简单。
要重命名本地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
这是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了。