Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why will git not let me delete a remote branch

Tags:

git

git-push

I have a local repository that I want to mirror to the remote 'websrv'. This used to work fine until I deleted a local branch. Now when I do

git push --mirror websrv

I get

remote: error: By default, deleting the current branch is denied, because the next
remote: error: 'git clone' won't result in any file checked out, causing confusion.
remote: error: 
remote: error: You can set 'receive.denyDeleteCurrent' configuration variable to
remote: error: 'warn' or 'ignore' in the remote repository to allow deleting the
remote: error: current branch, with or without a warning message.
remote: error: 
remote: error: To squelch this message, you can set it to 'refuse'.
remote: error: refusing to delete the current branch: refs/heads/ecoli-moments
To [email protected]:~/baki_tracking.git
 ! [remote rejected] ecoli-moments (deletion of the current branch prohibited)

The branch 'ecoli-moments' points to the same commit as master, local and remote.

What can I do about this so that the remote branch will be properly deleted?

Update:

The remote repository is bare, I checked the directory on the server (the config file has bare=true).

like image 422
Marius Avatar asked Jan 21 '14 10:01

Marius


People also ask

Can not delete branch in git?

Open a Git BASH window or Command Window in the root of your Git repository. If necessary, use the git switch or checkout command to move off the branch you wish to delete. Issue the git branch –delete <branchname> command to delete the local branch. Run the git branch -a command to verify the local Git branch is ...

What is the command to delete a branch in your remote repository?

You'll often need to delete a branch not only locally but also remotely. To do that, you use the following command: git push <remote_name> --delete <branch_name>. The branch still exists locally, though.


2 Answers

Even a bare repository has a current branch. It is the one that will be checked out by default when you clone the repository. Git does not want to delete it and is telling you why.

The default branch is the special HEAD reference. Modify it to point to something that is expected to always exist, or to point to a revision. See also How does origin/HEAD get set?.

You'll have to change what HEAD points to manually using git symbolic-ref(1) on the server.

Or you can set the option mentioned in the error to false (git config receive.denyDeleteCurrent false on the server); if it's just a backup (--mirror is mostly appropriate only for backup), the default branch does not really matter.

like image 101
Jan Hudec Avatar answered Nov 12 '22 13:11

Jan Hudec


This seems to be a non bare repository. If that is the case, any push operation to the checked out branch on non bare repo will not work, git doesn't allow that.

So you essentially need to check out a different branch and then push to the repo.

From Jan's comment, A useful method is to check no branch at all, that is go to the state known as detached HEAD where the HEAD ref points to specific revision rather than to a branch.

The advantage of this is that if you make more commits to different branches in future, you don't have to check out different branch every time this error is thrown.

A better way to avoid this though, if you are doing this for backups only, is to use a bare repo next time onwards.

like image 44
Anshul Goyal Avatar answered Nov 12 '22 15:11

Anshul Goyal