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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44657989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:02:22  来源:igfitidea点击:

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

gitgitlab

提问by smcs

I'm trying to delete a branch both locally and in a remote GitLab repository. Its name is origin/feat. I tried git push --delete origin feat. Git complains:

我正在尝试在本地和远程 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

OK makes sense, so I tried switching to origin/masterwith git checkout masterand it tells me: Already on 'master'. Does the current branch also need to be set in the remote directory? How would I do that?

OK 是有道理的,所以我尝试切换到origin/masterwith git checkout master,它告诉我:Already on 'master'. 当前分支是否也需要在远程目录中设置?我该怎么做?

采纳答案by torek

Edit (per comments by OP—I have not used GitLab): GitLab has a web interface with dropdowns. 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.

编辑(根据 OP 的评论——我没有使用过 GitLab):GitLab 有一个带有下拉菜单的 Web 界面。您需要设置视图(而不是项目视图)下的那个。在“默认分支”下的“设置”视图中选择一个分支,然后单击“保存更改”以设置服务器上的当前分支。

Details

细节

You have the right idea, but you have to remember that there are tworepositories—two Gits—involved.

您的想法是正确的,但您必须记住,涉及到两个存储库——两个 Git。

Any time you get text prefixed with remote:, this means the text is coming from the other Git. So when you have your Git ask the other Git to delete feat, it's the other Git complaining that featis the current branch.

任何时候你得到带有前缀remote:的文本,这意味着文本来自另一个 Git。所以当你让你的 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, cdto the repository directory, and run git checkout. But there's a hitch or two with push-able repositories on servers:

通常,与对任何存储库执行此操作的方式相同:登录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 --barerepository, so that you can't use git checkoutdirectly. The trick here is to use git symbolic-refto 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-refoperation for you.

  • 它在服务器上。你甚至允许登录吗?如果没有,您需要一些替代方案(特定于服务器)。
  • 它可能是一个--bare存储库,因此您无法git checkout直接使用。这里的技巧是git symbolic-ref用来更新HEAD

    git symbolic-ref HEAD refs/heads/master
    

    当然,这假设您可以登录(请参阅第一点)。例如,如果有一个 Web 界面可以让您更改远程上的当前分支,则它必须git symbolic-ref为您执行此操作。

回答by Charles

Try

尝试

git push origin --delete feat

git push origin --delete feat

回答by kk.

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

回答by Gabriel Vianna

I had the same issue when I wanted to delete the master from origin.

当我想从原点删除主人时,我遇到了同样的问题。

Assuming you want to delete the master, I resolved the problem in 3 steps:

假设您要删除 master,我分 3 个步骤解决了该问题:

  1. Go to the GitLab page for your repository, and click on the “Settings” button.

  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.

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

  2. Default Branch 中,将默认分支从 master 切换到其他分支。

  3. Protected Branches 中,如果有任何保护,则取消对 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.

如果它不是您要删除的 master,只需对所需的分支执行相同的步骤即可。

回答by ThorSummoner

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

回答by James Siva

To delete branch

删除分支

git branch -D registryDB(a Git branch name)

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

git push origin :registryDB

git push 原点:registryDB