我如何重命名尚未被推到远程仓库的本地分支 ?

相关:

重命名本地和远程 Git 仓库的主分支 ?


当前回答

所有你必须做的是三步:

在.git/refs/head 下给旧分支以.git/refs/head 下的新名称 给旧分支以.git/log/refs/head 下的新名称 更新 .git/HEAD 以导致您的新分支名称 。

其他回答

另一个选项是完全不使用命令行。 Git GUI 客户端, 如 SourceTree , 拿走了导致像此这样的问题成为Stack 溢流中最受关注的问题的学习周期曲线 / 疼痛 。

在 SourceTree 中,右键单击左侧“ Branches” 窗格中的任何本地分支并选择“ 重命名... ” 。

一个简单的方法来做到这一点:

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

更多,看看这个。

更新2023年最新情况

在开始之前, 请确定您已经选择了您想要重命名的分支 :

git checkout old-name

如果您想要看到本地所有的分支,请使用以下命令:

git branch --list

当你们清清楚楚时,

使用 Git 重命名分支命令需要您在您的命令中添加一个 - m 选项 : git 分支 - m 新名称 您也可以使用以下两个命令从另一个分支重命名本地分支 : git checkout master git 分支 - m 旧名新名称 最后, 此命令将列出所有本地和远程分支, 以核实它是否被重新命名 : git 分支 - a

虽然无法直接重命名一个远程分支, 但重命名一个分支的过程包括两个简单步骤:

要开始, 您需要按照前几个步骤重新命名本地分支 。 2. 然后删除旧分支, 并按下新分支 。 您可以在以下命令下轻松地做到这一点 : git 推推源 : old- name new - name 重设您新本地分支的上游分支 , 您将全部设置为: git 推源 - u new - name

重命名本地分支 : git 分支 -m <old_branch_name_name> <new_branch_name> 按下新分支 : git 推 -- set- 上流来源 < new_ branch_ name>

吉特・伊特的反派命令 试试这个 对我有用

默认分支已被重新命名 ! { oldBranchName} 现在命名为{ newBranchName} 如果您有本地克隆, 您可以运行以下命令来更新它 。

git branch -m {oldBranchName} {NewBranchName}
git fetch origin
git branch -u origin/{NewBranchName} {NewBranchName}
git remote set-head origin -a