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

相关:


当前回答

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

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 branch -m old_branch_name new_branch_name

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

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

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

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

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

  1. 切换到需要重新命名的分支
  2. git branch -m <new_name>
  3. git push origin :<old_name>
  4. git push origin <new_name>:refs/heads/<new_name>

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

git branch --unset-upstream

1. 重命名

如果是您的当前分支, 只需做

git branch -m new_name

如果它是另一个分支, 您想要重命名

git branch -m old_name new_name

2. 跟踪一个新的远程分支

- 如果您的分支被推动, 那么在重新命名后, 您需要从远程 Git 仓库删除它, 并要求您的新本地端跟踪新的远程分支 :

git push origin :old_name
git push --set-upstream origin new_name

另一个选项是根本不使用命令行。 Git GUI 客户端,例如源树树导致像这个这样的问题 成为Stack overflow最受关注的问题之一。

在 SourceTree 中,右键单击左侧“ Branches” 窗格中的任何本地分支并选择“ 重命名... ” 。

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