git 远程分支重命名

在git中重命名远程分支,其实就是先删除远程分支,然后重命名本地分支,再重新提交一个远程分支。

例如下面的例子中,我需要把 devel 分支重命名为 develop 分支:

$ git branch -av
* devel                             752bb84 Merge pull request #158 from Gwill/devel
  master                            53b27b8 Merge pull request #138 from tdlrobin/master
  zrong                             2ae98d8 modify CCFileUtils, export getFileData
  remotes/origin/HEAD               -> origin/master
  remotes/origin/add_build_script   d4a8c4f Merge branch 'master' into add_build_script
  remotes/origin/devel              752bb84 Merge pull request #158 from Gwill/devel
  remotes/origin/devel_qt51         62208f1 update .gitignore
  remotes/origin/master             53b27b8 Merge pull request #138 from tdlrobin/master
  remotes/origin/zrong              2ae98d8 modify CCFileUtils, export getFileData

删除远程分支:

$ git push --delete origin devel
To [email protected]:zrong/quick-cocos2d-x.git
 - [deleted]         devel

重命名本地分支:

git branch -m devel develop

推送本地分支:

$ git push origin develop
Counting objects: 92, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (48/48), done.
Writing objects: 100% (58/58), 1.38 MiB, done.
Total 58 (delta 34), reused 12 (delta 5)
To [email protected]:zrong/quick-cocos2d-x.git
 * [new branch]      develop -> develop

然而,在 github 上操作的时候,我在删除远程分支时碰到这个错误:

$ git push --delete origin devel
remote: error: refusing to delete the current branch: refs/heads/devel
To [email protected]:zrong/quick-cocos2d-x.git
 ! [remote rejected] devel (deletion of the current branch prohibited)
error: failed to push some refs to '[email protected]:zrong/quick-cocos2d-x.git'

这是由于在 github 中,devel 是项目的默认分支。要解决此问题,这样操作:

  1. 进入 github 中该项目的 Settings 页面;
  2. 设置 Default Branch 为其他的分支(例如 master);
  3. 重新执行删除远程分支命令。

删除参考:https://blog.zengrong.net/post/1746.html

你可能感兴趣的:(git)