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

相关:


当前回答

高级 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 分支名称这是一篇关于这个的好文章。

实际上,你有三个步骤 因为本地分行服务器上有一个复制件 所以我们在服务器上的两个步骤上有一个步骤是本地分行:

  1. 重命名本地 :只需使用以下命令来重命名您当前分支的名称, 即使您检查了 :
    git branch -m <old-branch-name> <new-branch-name>
    
  2. 删除服务器 1 :使用以下命令删除服务器上的旧名称分支 :
    git push <remote-name[origin by default]> :<old-branch-name>
    
  3. 推新的一条:现在该是推动服务器上命名的新分支的时候了:
    git push -u <new-branch-name>
    

试图具体回答问题(至少是标题)。

您也可以重新命名当地当地树枝,但继续追踪遥控器上的旧名称。

git branch -m old_branch new_branch
git push --set-upstream origin new_branch:old_branch

现在,当你跑的时候git push远程old_branchref 与您的本地更新new_branch.

你必须知道并记住配置此配置。 但如果您没有选择远程分支名称的选择, 它可能会有用, 但是您不喜欢它( 我的意思是, 您有一个非常好也希望本地分行名称更明确。

使用抓取配置, 您甚至可以重命名本地远程引用 , 即refs/remote/origin/new_branchref 指向分支,事实上就是old_branch上 年 月origin然而,我非常不赞成这样做,因为你心智安全。

所有你必须做的是三步:

  1. 给旧分支下.git/refs/heads新名称
  2. 给旧分支下.git/logs/refs/heads新名称
  3. 更新.git/HEAD导致您新分支名称

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