背景
有时候分支命名出错,开发的过程中发现功能与名称不符,需要重命名分支名,
先来查看一下 git branch 命令里面是否提供了相应的功能:
git help branch
在输出中可以看到这样一行说明:
With a -m or -M option,
will be renamed to . If had a corresponding reflog, it is renamed to match , and a reflog entry is created to remember the branch renaming. If exists, -M must be used to force the rename to happen.
也就是说,本地分支可以通过 -m 参数重命名分支名,-M 参数强制重命名分支名。
对于远程分支我们删掉旧的,然后推一个新的上去就可以了。
步骤
0. 重命名本地分支
如果在要修改的分支上:
git branch -m new_name
如果不在要修改的分支上:
git branch -m old_name new_name
1. 删除旧分支对应的远程分支并将新名称的分支推送到远程
git push origin :old_name new_name
2. 重置新名称本地分支的上游分支
切换到新名称的分支上,然后
git push origin -u new_name
搞定!