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

相关:

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


当前回答

以下是重新命名分支的步骤 :

切换到需要重命名为 git 分支的分支 -m < new_ name> git 推力源 : <old_ name> git 推力源 < new_ name>:refs/heads/ < new_ name>

EDIT (12/01/2017): 确保您运行命令 Git 状态, 并检查新建的分支指向的是它自己的参考文献, 而不是旧的参考文献。 如果您发现旧的分支的引用, 您需要解开对上游的设置 :

git branch --unset-upstream

其他回答

高级 Git 用户可以手动重命名 :

Rename the old branch under .git/refs/heads to the new name

Rename the old branch under .git/logs/refs/heads to the new name

Update the .git/HEAD to point to yout new branch name

到目前为止,答案是正确的,但以下是一些补充信息:

人们可以安全地将分支名称重新命名为 '-m' (move) , 但必须小心使用 '- M' , 因为它迫使重命名, 即使已经有一个已有的分支名称相同 。 以下是“ gitt- branch” man page的节录 :

在 a - m 或 - M 选项下, < oldbranch > 将重新命名为 < newbranch > 。 如果 < oldbranch > 拥有相应的 reflog, 它将被重新命名为匹配 < newbranch > , 并创建一个 reflog 条目以记住分支重命名。 如果存在 < newbranch > , 则 - M 必须用来强制重命名 。

要在 GitHub 桌面上重命名本地分支, 请点击当前分支标签( 到当前仓库右侧) , 右单击您想要重命名的分支, 并点击重命名 。 然后您会被弹出提示到您可以重命名的地方 。

只要三步即可在远程和GitHub复制名称变化:

第 1 步 git 分支 - m 旧的Branchname new_ branchname

步骤 2 git 推进源 : old_ branchname new_ branchname

步骤 3 git 推进 - 设置上流源新分支名

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