Rename a Git branch locally and remotely? [duplicate]

一曲冷凌霜 提交于 2019-12-04 07:22:06

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/36999937/rename-a-git-branch-locally-and-remotely

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!