44

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?

7 Answers 7

70

Try

git push origin --delete <branch-name>
0
45

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
13

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.

1
  • 2
    Settings than "Repository" than Default Branch.
    – pungggi
    Commented Oct 14, 2020 at 9:36
8

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.

3
  • I see, thanks. GitLab does provide a web interface and there's a dropdown to switch between branches, but it doesn't seem to be linked to git since I keep getting the same error message.
    – smcs
    Commented Jun 21, 2017 at 7:30
  • @speedymcs: Odd. I wonder if there are server logs you can examine to find out what happens (or doesn't happen) when you use the web interface.
    – torek
    Commented Jun 21, 2017 at 14:20
  • 1
    I managed to delete the branch remotely now. In GitLab there are actually two dropdowns to switch the branch, one in the Project view, one in the Settings view of the respective project. Choosing a branch in the Settings view under "Default branch" and clicking "Save changes" makes git switch to that branch server-side. The dropdown in the project view just decides what files you're looking at in the interface.
    – smcs
    Commented Jun 22, 2017 at 8:22
0

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
0

May be the main branch is defined as the default branch. You need to define another default branch than the main. then you can delete it as stated above:

git push origin --delete main

check Change Default branch in gitlab to see how to remove default from main branch.

-1

To delete branch

git branch -D registryDB(a Git branch name)

git push origin :registryDB

Not the answer you're looking for? Browse other questions tagged or ask your own question.