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

相关:

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


当前回答

可能正如其他人所提到的那样,这将是分支命名中出现的情况不匹配。

如果你有这样的情况,我可以猜测 你在Windows上,这也将导致你:

$ git branch -m CaseSensitive casesensitive
fatal: A branch named 'casesensitive' already exists.

然后,你必须做一个中间步骤:

$ git branch -m temporary
$ git branch -m casesensitive

仅此而已。

其他回答

另一个选项是完全不使用命令行。 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

更多,看看这个。

对于 Git GUI 用户来说,它不可能简单得多。 在 Git GUI 中, 从“ 重新命名分支” 对话框创建的“ 重新命名分支” 对话框的向下列表中选择分支名称 : 重新命名, 输入新名称, 单击“ 重新命名 ” 。 我已经突出显示要在哪里找到向下下载列表 。

高级 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

试图具体回答问题(至少是标题)。

您也可以重命名本地分支,但继续跟踪远程的旧名称。

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

现在,当您运行 Git 推时, 远程的老管区ref 用您的本地新管区更新 。

您必须知道并记住这个配置。 但如果您没有选择远程分支名称的选择, 它可能会有用, 但是您不喜欢它( 您有一个很好的理由不喜欢它! ) 并且更喜欢您本地分支更清晰的名称 。

使用抓取配置, 您甚至可以重命名本地远程参考 。 也就是说, 拥有分支的 refs/ remote/ broot/ new_ branch ref 指针, 事实上就是原发的旧分队。 但是, 我非常不鼓励您这样做, 以保障您心灵的安全 。