10

I'm experimenting with some githooks, so I have setup a local bare repository with a symlink from hooks to the hooks stored elsewhere.

I have pushed the master branch to the git repo and, of course, the hook failed. :)

I want to reset the git repo to nothing, without deleting it and having to recreate the symlink etc.

How do I delete the master branch, seeing as it is the only branch in the repository?

$ git branch -d master error: Cannot delete the branch 'master' which you are currently on. 

2 Answers 2

19

To remove the master ref, use git update-ref -d refs/heads/master.

Why shouldn't you rm /refs/heads/master?

packed-refs could exist, so rm sometimes doesn't work as expected.

BTW, what's the point to reset if you could just create a new empty repo instead? Just use git init --bare repo.git.

Sign up to request clarification or add additional context in comments.

7 Comments

rm refs/heads/master is shorter than git update-ref -d refs/heads/master; why should I use the second?
@AlexChamberlain Because there is also packed-refs could exist, so rm sometimes doesn't work as expected. And yes, thanks about refs/heads/ prefix, I've forgot about it.
git init --bare repo.git doesn't delete the branches currently there.
@AlexChamberlain I mean that instead of deleting everything from existing repo, you could just delete it and create a new pristine one.
Because I've setup a symlink for the hooks directory.
|
1

Normally, you don't need to remove a branch, you'd use git reset --hard REV to set it to the desired new revision. However, if I understand yuo correctly, you want to reset it to "nothing", i.e. to the state as it was right after git init was first invoked. git doesn't seem to allow you to do that, but you can get a similar effect by simply removing .git/heads/refs/master. Here is a demonstration in a newly created repo:

[~/x]$ git init Initialized empty Git repository in /home/author/x/.git/ [~/x]$ touch a [~/x]$ git add a [~/x]$ git commit -m foo [master (root-commit) 5fcc99c] foo 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 a [~/x]$ git log commit 5fcc99cc396cf5bc2c2fa9edef475b0cc9311ede Author: ... Date: Mon Sep 3 12:40:15 2012 +0200 foo 

Here you'd want to do this, but git doesn't allow it:

[~/x]$ git reset --hard HEAD^ fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree. Use '--' to separate paths from revisions 

However, you can do this instead:

[~/x]$ rm .git/refs/heads/master 

Check that it worked by committing something

[~/x]$ touch b [~/x]$ git add b [~/x]$ git commit -m 'new history' [master (root-commit) 0e692b9] new history 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 a create mode 100644 b [~/x]$ git log commit 0e692b9bb77f526642dcdf86889ec15dfda12be0 Author: ... Date: Mon Sep 3 12:40:52 2012 +0200 new history [~/x]$ git branch * master 

2 Comments

Removing the ref works for me. Of course, this doesn't remove the commits, but it serves the same purpose and the commits would be removed by a git gc if desired.
rm is a dirty solution, See my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.