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

相关:


当前回答

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

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

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

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

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

仅此而已。

其他回答

git branch -m old_branch_name new_branch_name

上述命令将改变您的分支名称, 但你必须非常小心地使用重新命名的分支, 因为它仍然会指与其相关的旧的上游分支, 如果有的话 。

如果您想要在本地分支重新命名为新分支后将一些更改推入母版( 例如名称) :

git push origin new_branch_name:master(现在更改将转到母版分支, 但您的本地分支名称是新_ branch_ name)

详情见 " 。如何在 Git 中重新命名您的本地分支名称."

要重命名当前分支( 除独立 HEAD 状态外) 您也可以使用此别名 :

[alias]
    mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'

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

如果您不想将分支推到远程服务器, 这个示例将很有用 :

假设你有一个现有的分支 叫做"我的热能特长" 你想把它改名为"特长15"

首先,你想改变你的地方分支。

git branch -m my-hot-feature feature-15

更多信息,请访问地方和远程重命名Git的一个分支.

之前所有的答案都在谈论git branch -m。 当然, 操作很容易, 但对我来说, 可能很难记住另一个 Git 命令 。 所以我试图完成我熟悉的指令。 是的, 你可能猜到了 。

我用的是git branch -b <new_branch_name>。如果你不想保存旧的分支,现在你可以执行git branch -D <old_branch_name>要删除它。

我知道这或许有点无聊, 但更容易理解和记住。我希望这对你有益。