为什么git不允许我删除远程分支

时间:2014-01-21 10:24:24

标签: git git-push

我有一个本地存储库,我想镜像到远程'websrv'。这曾经工作正常,直到我删除了一个本地分支。现在我做的时候

git push --mirror websrv

我得到了

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/ecoli-moments
To git@141.89.117.199:~/baki_tracking.git
 ! [remote rejected] ecoli-moments (deletion of the current branch prohibited)

分支'ecoli-moments'指向与master,local和remote相同的提交。

我该怎么办才能正确删除远程分支?

更新

远程存储库是裸的,我检查了服务器上的目录(配置文件有bare = true)。

2 个答案:

答案 0 :(得分:15)

即使是裸存储库也有当前分支。克隆存储库时,默认情况下将检出该选项。 Git不想删除它,并告诉你原因。

默认分支是特殊HEAD引用。将其修改为指向预期始终存在的内容,或指向修订。另请参阅How does origin/HEAD get set?

您必须使用服务器上的git symbolic-ref(1)手动更改HEAD指向的内容。

或者您可以将错误中提到的选项设置为false(服务器上的git config receive.denyDeleteCurrent false);如果它只是一个备份(--mirror主要仅适用于备份),默认分支并不重要。

答案 1 :(得分:1)

这似乎是一个非裸存储库。如果是这种情况,对非裸存储库中已检出分支的任何推送操作都将无效,git不允许这样做。

所以你基本上需要检查一个不同的分支,然后推送回购。

从Jan的评论来看,一个有用的方法是根本不检查分支,即进入称为detached HEAD的状态,其中HEAD ref指向特定修订而不是分支。

这样做的好处是,如果将来对不同的分支进行更多提交,则每次抛出此错误时都不必检查不同的分支。

避免这种情况的更好方法是,如果您仅为备份执行此操作,则下次使用bare repo