如何删除远程Git存储库上的HEAD分支?

Pab*_*blo 4 git git-tower

这是服务器上的远程Git存储库

[aaa@web48 proj.git]$ git ls-remote .
dfca707432eb53678b37026b160a4bdc7f1ac6c3    HEAD
dfca707432eb53678b37026b160a4bdc7f1ac6c3    refs/heads/master
1e09c37443ee758644a712e3c1a8b08b18a1f50d    refs/heads/placeholder
Run Code Online (Sandbox Code Playgroud)

我想删除HEAD/master分支.我怎么能在服务器上或远程执行此操作?我正在使用Tower客户端.

小智 10

如果远程分支当前是默认的HEAD分支,则无法删除它

HEAD远程裸仓库上的符号引用表示该仓库的默认分支.该repo的任何非裸克隆将在克隆后自动检出该分支.

因为这是默认设置,你不能像往常一样删除它,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'
Run Code Online (Sandbox Code Playgroud)

上面的错误消息指出您可以绕过安全检查以删除HEAD远程中的当前分支,但是我将向您展示如何更改默认分支,以便您仍然可以保留默认分支,但删除master你想要的.

从命令行更改默认HEAD分支

如果您有权访问远程控制台,可以更改远程仓库中的默认分支.如果您使用的是GitHub或Bitbucket等托管服务提供商,则应允许您通过其Web界面更改默认分支.

因此,如果您有权访问远程,请使用以下命令更改符号引用HEAD指向的分支:

git symbolic-ref HEAD refs/heads/<newDefaultBranch>
Run Code Online (Sandbox Code Playgroud)

更改GitHub或Bitbucket上的默认HEAD分支

正如我在上一节中已经提到的,HEAD如果您使用像GitHub或Bitbucket这样的托管服务,您可以通过Web界面更新远程仓库中的默认分支.

GitHub上

转到您的repo的"设置"选项卡,您将在顶部看到默认分支设置,

GitHub设置

到位桶

进入你的repo的设置选项卡,你会看到中间附近的默认分支设置,

Bitbucket设置

更新本地克隆对远程中默认分支的引用

一旦更新了远程裸仓库中的默认分支,您将需要更新该仓库的本地克隆认为HEAD远程中的默认分支指向的位置.你可以这样做

git remote set-head <remote> --auto

# Or shorter
git remote set-head <remote> -a
Run Code Online (Sandbox Code Playgroud)

您可以使用确认本地仓库已正确更新

$ git branch -r
  origin/HEAD -> origin/foo
  origin/foo
  origin/master
Run Code Online (Sandbox Code Playgroud)

现在您可以删除遥控器上的主分支

现在您已将HEAD远程控制台上的默认分支更改为主分支以外的其他分支,您将能够在远程控制台上删除它,

$ git push origin --delete master

To c:/Users/Keoki/Documents/GitHub/bare
 - [deleted]         master

# Older syntax
$ git push origin :master
Run Code Online (Sandbox Code Playgroud)

其他参考文献和文​​档