简体   繁体   中英

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. Its name is origin/feat . I tried git push --delete origin feat . Git complains:

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' . Does the current branch also need to be set in the remote directory? How would I do that?

Try

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. 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.

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 feat is the current branch.

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 . 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. 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.

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:

  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.

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.

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 push origin :registryDB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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