简体   繁体   English

为什么我不能删除远程GitLab存储库中的分支?

[英]Why can't I delete a branch in a remote GitLab repository?

I'm trying to delete a branch both locally and in a remote GitLab repository. 我正在尝试在本地和远程GitLab存储库中删除分支。 Its name is origin/feat . 它的名字是origin/feat I tried git push --delete origin feat . 我试过git push --delete origin feat Git complains: 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

OK makes sense, so I tried switching to origin/master with git checkout master and it tells me: Already on 'master' . OK很有意义,所以我尝试使用git checkout master切换到origin/master ,它告诉我: Already on 'master' Does the current branch also need to be set in the remote directory? 是否还需要在远程目录中设置当前分支? How would I do that? 我该怎么办?

Try 尝试

git push origin --delete feat git push origin --delete feat

To remove a local branch from your machine: 要从您的计算机中删除本地分支:

git branch -d <branch-name>

To remove a remote branch: 要删除远程分支:

git push origin :<branch-name>

In your case the above statements would be: 在您的情况下,上述陈述将是:

To remove a local branch from your machine: 要从您的计算机中删除本地分支:

git branch -d feat

To remove a remote branch: 要删除远程分支:

git push origin :feat

Edit (per comments by OP—I have not used GitLab): GitLab has a web interface with dropdowns. 编辑(OP-I的评论未使用GitLab):GitLab有一个带下拉菜单的Web界面。 You need the one under the Settings view (not the Project view). 您需要在“设置”视图下(而不是“项目”视图)下的那个。 Choose a branch in the Settings view under "Default branch" and click "Save changes" to set the current branch on the server. 在“默认分支”下的“设置”视图中选择一个分支,然后单击“保存更改”以在服务器上设置当前分支。

Details 细节

You have the right idea, but you have to remember that there are two repositories—two Gits—involved. 你有正确的想法,但你必须记住,有两个存储库 - 涉及两个Gits。

Any time you get text prefixed with remote: , this means the text is coming from the other Git . 每当你得到带有remote:前缀的文本时,这意味着文本来自另一个Git So when you have your Git ask the other Git to delete feat , it's the other Git complaining that feat is the current branch. 因此,当你让你的Git要求其他Git删除feat ,另一个Git抱怨该feat是当前的分支。

Hence: 因此:

Does the current branch also need to be set in the remote directory? 是否还需要在远程目录中设置当前分支?

Yes (well, "instead of" rather than "also"). 是的(好吧,“而不是”而不是“也”)。

How would I do that? 我该怎么办?

In general, the same way you do it with any repository: log in, cd to the repository directory, and run git checkout . 通常,与使用任何存储库执行此操作的方式相同:登录, cd到存储库目录,然后运行git checkout But there's a hitch or two with push-able repositories on servers: 但是在服务器上有可推送的存储库存在一两个障碍:

  • 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. 它可能是一个--bare存储库,因此您无法直接使用git checkout The trick here is to use git symbolic-ref to update HEAD : 这里的技巧是使用git symbolic-ref来更新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. 例如,如果有一个允许您更改遥控器上当前分支的Web界面,则必须为您执行此git symbolic-ref操作。

I had the same issue when I wanted to delete the master from origin. 当我想从原点删除master时,我遇到了同样的问题。

Assuming you want to delete the master , I resolved the problem in 3 steps: 假设您要删除主服务器 ,我通过3个步骤解决了问题:

  1. Go to the GitLab page for your repository, and click on the “ Settings ” button. 转到存储库的GitLab页面,然后单击“ 设置 ”按钮。

  2. In Default Branch , switch the default branch from your master to other one. 在“ 默认分支”中 ,将默认分支从主分支切换到另一分支。

  3. In Protected Branches , if there's any protection, unprotect the master. 受保护的分支中 ,如果有任何保护,请取消保护主数据。

Then you try again to delete the branch. 然后再次尝试删除分支。

If it's not the master you want to delete, just follow the same steps with the desired branch. 如果它不是您要删除的主数据库,只需按照所需分支执行相同的步骤即可。

if you are trying todelete multiple branches, any protected branch (eg the default branch, usually master ) will cause the whole request to fail, so maybe try delete them one at a time, or excluding known protected branches. 如果你试图删除多个分支,任何受保护的分支(例如默认分支,通常是master分支)将导致整个请求失败,因此可以尝试一次删除一个,或者排除已知的受保护分支。

For example, I was trying to delete merged branches with 例如,我试图删除合并的分支

$ 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

To delete branch 删除分支

git branch -D registryDB(a Git branch name) git branch -D registryDB(一个Git分支名称)

git push origin :registryDB git push origin:registryDB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM