47

I actually want to remove a branch in the bare repository i am working with, but this task hits a dead end because I cannot switch away from the master repository without a 'work tree' which a bare repository does not have.

When I run git branch -d master the output is:

error: Cannot delete the branch 'master' which you are currently on.

So I try to switch to another branch called 'develop' by running git checkout develop and the output is:

fatal: This operation must be run in a work tree
Ankur
  • 2,586
  • 2
  • 20
  • 25
  • 1
    This looks like a duplicate of http://stackoverflow.com/q/3301956/4918, but is exactly my use-case and much better to read. Thus I +1 here and do not flag, just link. – David Schmitt Aug 30 '11 at 14:00
  • 3
    Does this answer your question? [Git: Correct way to change Active Branch in a bare repository?](https://stackoverflow.com/questions/3301956/git-correct-way-to-change-active-branch-in-a-bare-repository) – MrTux Nov 13 '19 at 07:08

1 Answers1

71

Try this instead of git checkout:

git symbolic-ref HEAD refs/heads/develop

Then you should be able to delete master.

cdhowie
  • 133,716
  • 21
  • 261
  • 264
  • `git clone --bare` does not create a `refs/heads/master`. Short of editing `HEAD` manually, how do I switch the symbolic ref for `HEAD` back to `master` if no such head exists? – Dmitry Minkovsky Sep 03 '13 at 16:41
  • @dimadima The same way. If you look in `packed-refs` you should see that it contains a definition for `refs/heads/master`. – cdhowie Sep 03 '13 at 18:38
  • See also this answer by @VonC explaining about [symbolic-ref](http://stackoverflow.com/questions/3301956/git-correct-way-to-change-active-branch-in-a-bare-repository#3302018). – Ted Sep 23 '15 at 15:01