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

相关:


当前回答

仅三个步骤,以复制名称的更改remote以及基特胡卜人的故事:

步骤1git branch -m old_branchname new_branchname

步骤2git push origin :old_branchname new_branchname

步骤3git push --set-upstream origin new_branchname

其他回答

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

git 分支 - m 新名称

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

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

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

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

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

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

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

采用-m或-m或-M选项,<oldbranch>将重新命名为<newbranch>。如果<oldbranch>具有相应的卷号, 重新命名为匹配<newbranch>,将创建一个重写条目以记住分支重新命名。如果<newbranch>存在, -M 必须用于强制重命名 。

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

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

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

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

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

仅此而已。