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

相关:

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


当前回答

高级 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         # 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 分支名称是一个很好的文章 。

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

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

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

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

若想了解更多信息,请访问Git的本地和远程重命名分部。

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

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

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

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

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

仅此而已。

有关这一程序的更多详情。

如何在 Git 中重命名本地分支

要重命名当前分支, 请确认您已经检查过当前分支的名称, 并且正在使用您想要重命名的分支 。 git checkout oldbranch 然后 git 分支 - m newbranch 如果您想要, 您可以在另一个分支工作时重新命名一个分支 。 git 分支 - m 旧分支 newbranch

如何重命名 Git 中的远程分支

如果其他用户使用此分支并承诺执行此分支, 您应该在将其重新命名为本地之前将其拉动 。 这样可以确保您的本地仓库得到更新, 并确保其他用户所做的更改不会丢失 。

首先,我们需要从远程仓库删除旧的分机, 并将 enwbranch 推到远程仓库。 git 推车源头 -- delette oldbranch Now 我们将使用 - u (设定的上游) 选项将新分机推到远程 。 git 推车源头 - u newbranch

一个简单的方法来做到这一点:

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

更多,看看这个。