为什么我无法在远程GitLab存储库中删除分支?

44
我试图同时删除本地分支和远程 GitLab 仓库中的一个名为origin/feat的分支。我尝试使用git push --delete origin feat 命令,但是 Git 报错:
remote: error: By default, deleting the current branch is denied, because the next
remote: 'git clone' won't result in any file checked out, causing confusion.
remote: 
remote: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: current branch, with or without a warning message.
remote: 
remote: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/feat

好的,明白了,那我就尝试使用 git checkout master 切换到origin/master 分支,结果告诉我:Already on 'master'。当前分支是否也需要在远程目录中设置?我应该如何设置呢?

6个回答

65
尝试
git push origin --delete <branch-name>

2
其中'feat'是分支名称!+1 - Codetard
这是问题中提到的分支名称,通用答案应该是 git push origin --delete <:branch_name> - Charles

45
要从您的计算机中删除本地分支:
git branch -d <branch-name>

移除远程分支的方法:

git push origin :<branch-name>

在你的情况下,上述语句将是:

从你的计算机中删除一个本地分支:

git branch -d feat

删除远程分支:

git push origin :feat

12

当我想从源中删除主分支时,我遇到了同样的问题。

假设您要删除主分支,我通过以下三个步骤解决了这个问题:

  1. 进入您的存储库的GitLab页面,然后单击“设置”按钮。

  2. 默认分支中,将默认分支从您的主分支切换到其他分支。

  3. 受保护的分支中,如果有任何保护,则取消对主分支的保护。

然后再尝试删除分支。

如果您要删除的不是主分支,请按照所需分支执行相同的步骤。


2
设置 -> 存储库 -> 默认分支。 - pungggi

8
编辑(根据OP的评论 - 我没有使用GitLab):GitLab有一个带下拉菜单的Web界面。您需要在“设置”视图(而不是“项目”视图)下选择一个分支。在“默认分支”下的设置视图中选择一个分支,然后点击“保存更改”以将当前分支设置在服务器上。
详细信息:
你想得对,但你必须记住有两个仓库 - 两个Git参与其中。
每当您获取以“remote:”为前缀的文本时,这意味着文本来自另一个Git。所以当您的Git要求其他Git删除“feat”时,那是另一个Git抱怨“feat”是当前分支。
因此:
“当前分支也需要在远程目录中设置吗?”
是的(取代而不是“也”)。
“我该如何做?”
一般来说,与任何存储库一样处理:登录,cd到存储库目录,然后运行git checkout。但是,在可推送到服务器上的存储库中有一些问题。
  • It's on the server. Are you even allowed to log in? If not, you need some alternative (which is server-specific).
  • It's probably a --bare repository, so that you can't use git checkout directly. The trick here is to use git symbolic-ref to update HEAD:

    git symbolic-ref HEAD refs/heads/master
    

    Of course, this assumes you can log in (see first point). If there is, say, a web interface that lets you change the current branch on the remote, it is going to have to do this git symbolic-ref operation for you.


我明白了,谢谢。GitLab确实提供了一个Web界面,并且有一个下拉菜单可以在分支之间切换,但似乎它并没有与git链接,因为我一直收到相同的错误消息。 - smcs
@speedymcs:奇怪。我想知道是否有服务器日志可以检查,以找出在使用Web界面时发生(或未发生)什么情况。 - torek
1
我现在成功地远程删除了分支。在GitLab中,实际上有两个下拉菜单可以切换分支,一个在项目视图中,另一个在相应项目的设置视图中。在“默认分支”下的设置视图中选择一个分支并点击“保存更改”会使git在服务器端切换到该分支。项目视图中的下拉菜单只决定您在界面中查看哪些文件。 - smcs

0

如果您正在尝试删除多个分支,任何受保护的分支(例如默认分支,通常是master)都会导致整个请求失败,因此可能建议您一次只删除一个分支,或者排除已知的受保护分支。

例如,我尝试使用以下命令删除已合并的分支:

$ git fetch  mygitlabremote --prune; \
    git branch --remotes --list mygitlabremote/* --merged \
    | cut -d/ -f2 | grep -v master \
    | xargs git push mygitlabremote  --delete
$ git fetch  mygitlabremote --prune

-1
删除分支:
git branch -D registryDB(Git 分支名称)
git push origin :registryDB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接