如何在本地和远程重命名git分支? [英] How to rename a git branch locally and remotely?

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

问题描述

有没有办法在本地重命名git分支并将其推送到服务器上,即使已经有很多提交到远程的提交?



或者我们有创建一个新的并删除旧的并重复在远程服务器上的操作?

解决方案

是,



特征 move 存在以在本地重命名分支

  git分支 - 移动< old_name> < NEW_NAME> 

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

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




- set-upstream 是可选的,它将新的本地分支配置为跟踪被推送的分支



可以使用以下简写:




  • 本地移动( - 移动):

      git branch -m< old_name> < NEW_NAME> 


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

      git push origin [-u]< new_name> 


  • 删除( - 删除):

      git push origin -d< old_name> 




NB。



感谢torek的评论:顺便提一下,值得一提的是,您应该


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

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

< #2的原因主要在于效率:它避免了必须将对象重新复制到上游回购站点,从而删除分支删除时的提交(大多数裸露的存储库都是这样做的,大多数接受推送的存储库都是裸露的)


Is there a way to rename a git branch locally and push it on the server, even if there is already many commits pushed to the remote ?

Or we have to create a new and delete the old and repeat the operation on the remote server ?

解决方案

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>

--set-upstream is optional, it configure the new local branch to track the pushed one

You can use the following shorthands:

  • move locally (--move) :

    git branch -m <old_name> <new_name>
    

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

    git push origin [-u] <new_name>
    

  • delete (--delete) :

    git push origin -d <old_name>
    

NB.

Thanks to torek's comment:

Worth mentioning, by the way, is that you should

  1. notify other users who share the upstream that you will be doing this, and
  2. do this in the order shown (set new name, then delete old).

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

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分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆