在本地和远程重命名Git分支

在本地和远程重命名Git分支

本文介绍了在本地和远程重命名Git分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使已经有很多提交推送到远程分支,是否有办法在本地重命名Git分支并将其推送到远程分支?

Is there a way to rename a Git branch locally and push it to the remote branch, even if there are already many commits pushed to the remote branch?

或者,是否有必要创建一个新的本地分支,删除旧的本地分支,然后在远程存储库上重复该操作?

Or, is it necessary to create a new local branch, delete the old local branch, and then repeat the operation on the remote repository?

推荐答案

功能move存在,可以在本地重命名分支

Yes,

the feature move exists to rename the branch locally

git branch --move <old_name> <new_name>

但是要推送它,您必须删除旧的并推送新的

but to push it, you must delete the old and push the new

git checkout <new_name>
git push origin [--set-upstream] <new_name>
git push origin --delete <old_name>

您可以使用以下速记:

  • 本地移动(-移动):

    You can use the following shorthands:

    • move locally (--move) :

        git branch -m <old_name> <new_name>
      

    • 推送新分支(--set-upstream,可选):

    • push new branch (--set-upstream, optional) :

        git push origin [-u] <new_name>
      

    • 删除(--delete):

    • delete (--delete) :

        git push origin -d <old_name>
      

    • 感谢torek的评论:

      Thanks to torek's comment:

      顺便说一句,值得一提的是

      Worth mentioning, by the way, is that you should

      1. 通知共享上游的其他用户您将要执行此操作,并且
      2. 按照显示的顺序进行操作(设置新名称,然后删除旧名称).

      #1的原因是这些用户需要调整.

      The reason for #1 is that those users will need to adjust.

      #2的原因主要是效率:它避免了将对象重新复制到上游存储库的操作,该上游存储库删除了分支删除上的提交(大多数裸存储库都这样做,并且大多数接受推送的存储库都是裸存储的)

      The reason for #2 is mainly just efficiency: it avoids having to re-copy objects to an upstream repo that drops commits on branch deletion (most bare repositories do that, and most repositories that accept pushes are bare)

      这篇关于在本地和远程重命名Git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:53