如果当前默认分支是远程分支,那么您无法删除它
在远程裸仓库中,HEAD
符号引用代表该仓库的默认分支。那个仓库的非裸克隆将在克隆后自动检出该分支。
由于它是默认值,您不能像通常一样直接删除它,Git 不会让你这样做:
$ git push origin --delete master
remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error:
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error:
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/master
To c:/Users/Keoki/Documents/GitHub/bare
! [remote rejected] master (deletion of the current branch prohibited)
error: failed to push some refs to 'c:/Users/Keoki/Documents/GitHub/bare'
上面的错误消息指出,你可以绕过安全检查从而删除远程当前的
HEAD
分支,但我将向你展示如何更改默认分支,这样你就仍然可以保留一个默认分支,但也可以像你想要的那样删除
master
分支。
通过命令行更改默认的HEAD分支
如果你有访问远程仓库的权限,你可以更改远程仓库的默认分支。如果你使用的是像GitHub或Bitbucket这样的托管提供商,他们应该允许你通过他们的网页界面来更改默认分支。
因此,如果你有访问权限,使用以下命令更改符号引用
HEAD
所指向的分支:
git symbolic-ref HEAD refs/heads/<newDefaultBranch>
在GitHub或Bitbucket上更改默认的HEAD分支
正如我在前面提到的,如果你使用像GitHub或Bitbucket这样的托管服务,你可以通过Web界面更新远程仓库中的默认HEAD
分支。
GitHub
转到您的存储库的“设置”选项卡,您会看到默认分支设置就在顶部,
Bitbucket
转到您的存储库的“设置”选项卡,您会看到默认分支设置就在中间附近,
更新本地克隆的引用以指向远程的默认分支
一旦您已经更新了远程裸仓库中的默认分支,您需要更新本地克隆库认为远程的默认HEAD
分支指向哪里。你可以使用下面的命令来完成:
git remote set-head <remote> --auto
git remote set-head <remote> -a
您可以使用以下命令确认本地仓库已经正确更新:
$ git branch -r
origin/HEAD -> origin/foo
origin/foo
origin/master
现在您可以删除远程的主分支
既然您已经将远程默认的 HEAD
分支更改为不是主分支,您就可以在远程上删除它了。
$ git push origin --delete master
To c:/Users/Keoki/Documents/GitHub/bare
- [deleted] master
# Older syntax
$ git push origin :master
更多参考资料和文档