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

相关:

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


当前回答

这里有三个步骤:命令, 您可以在终端内调用, 更改分支名称 。

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

其他回答

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

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

更多,看看这个。

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

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

在 a - m 或 - M 选项下, < oldbranch > 将重新命名为 < newbranch > 。 如果 < oldbranch > 拥有相应的 reflog, 它将被重新命名为匹配 < newbranch > , 并创建一个 reflog 条目以记住分支重命名。 如果存在 < newbranch > , 则 - M 必须用来强制重命名 。

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/refs/head 下给旧分支以.git/refs/head 下的新名称 给旧分支以.git/log/refs/head 下的新名称 更新 .git/HEAD 以导致您的新分支名称 。

以下是重新命名分支的步骤 :

切换到需要重命名为 git 分支的分支 -m < new_ name> git 推力源 : <old_ name> git 推力源 < new_ name>:refs/heads/ < new_ name>

EDIT (12/01/2017): 确保您运行命令 Git 状态, 并检查新建的分支指向的是它自己的参考文献, 而不是旧的参考文献。 如果您发现旧的分支的引用, 您需要解开对上游的设置 :

git branch --unset-upstream