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

相关:

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


当前回答

使用此命令重命名分支 :

git branch -m [old_branch_name] [new_branch_name]

- m: 它重命名/ 移动分支。 如果已经有一个分支, 您将会出错 。

如果已经有一个分支, 您想要用该分支重命名, 请使用 :

 git rename -M [old_branch_name] [new_branch_name]

有关帮助的更多信息,请在终端中使用此命令 :

git branch --help

man git branch

其他回答

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

如果你有这样的情况,我可以猜测 你在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] [new_branch]

如果您想要删除旧的远程分支,请运行 :

git push origin :[old_branch]

如果您想要删除旧的远程分支并创建一个新的远程分支,请运行 :

git push origin :old_branch new_branch

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

2.9.2 Git 版本

如果您想要更改本地分支的名称, 请在 :

git branch -m new_name

如果您想要更改另一分支的名称 :

git branch -m old_name new_name

如果您想要将其它分支的名称更改为已经存在的名称 :

git branch -M old_name new_name_that_already_exists

注意: 最后一个命令是破坏性的, 将会重新命名您的分支, 但是您会丢失旧分支, 并且会丢失旧分支, 因为分支名称必须是独一无二的 。

改变本地分支很容易...

如果您在树枝上, 您想要更改名称, 只需这样做 :

git branch -m my_new_branch

否则,如果你是主人 或任何其他分支 除了你想改名的分支, 简单做:

git branch -m my_old_branch my_new_branch

另外,我在下面创建图像, 以在命令行的操作中显示此图像。 在这种情况下, 您在主分支中, 例如 :