17

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 git@141.89.117.199:~/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).

Marius
  • 651
  • 1
  • 6
  • 14
  • 1
    Yes, but as far as I can see it tells me how to deal with the error message and not the cause. – Marius Jan 21 '14 at 17:00
  • 1
    My guess is you are not pushing to a `bare` repository and you are not allowed to delete the branch currently checked out on `webserv`. – Nils Werner Jan 21 '14 at 18:58
  • @NilsWerner: The error talks about clone, not working directory. It's not the one non-bare repositories give when pushing into checked out branch. But when I looked up documentation for the option it does talk about non-bare repositories. I'd still trust the error message more than the doc and think the repo may be properly bare though. – Jan Hudec Jan 22 '14 at 05:53
  • Marius, would you be so kind and update the question with whether the repository (on webserv) is bare or not? – Jan Hudec Jan 22 '14 at 05:54

2 Answers2

23

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.

Community
  • 1
  • 1
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • git symbolic-ref did it, thank you. Although I still don't know how I managed to change the HEAD reference. Maybe I did it playing with gitg... – Marius Jan 22 '14 at 23:25
  • @Marius: It may have happened to be this when you first populated the repository or something. – Jan Hudec Jan 23 '14 at 07:54
  • How can I set this on the server to false? – rubo77 Oct 01 '15 at 12:45
  • @rubo77, it depends on how the server is managed. Git does not provide access to it, but some management tools like gitolite have interface for setting options in the server repositories. – Jan Hudec Oct 01 '15 at 12:55
1

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.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 2
    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. Git could probably switch to that state itself on such push, but noone cared about this use-case enough to implement it as far as I know. – Jan Hudec Jan 21 '14 at 10:36
  • I did think of that, but it made more sense to use a bare repo for such purpose. Will add your comment to the answer. – Anshul Goyal Jan 21 '14 at 10:37
  • I agree that bare repo is better. But if the work tree is needed for some reason or it is now difficult to switch, the detached head is an option. – Jan Hudec Jan 21 '14 at 10:45
  • Reading the error carefully the repository actually can be bare! – Jan Hudec Jan 21 '14 at 22:51
  • @JanHudec Bare repo throwing that error.. Hmm can't think of a case when this would be happening. – Anshul Goyal Jan 22 '14 at 03:56
  • Read the error again. It says the ref can't be deleted because `clone` would not know what to check out. So it does not have to be checked out. – Jan Hudec Jan 22 '14 at 06:17
  • The repository is in fact bare, see update to question. – Marius Jan 22 '14 at 10:51