Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the only branch in a bare `git` repository?

Tags:

git

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.
like image 984
Alex Chamberlain Avatar asked Sep 03 '12 10:09

Alex Chamberlain


2 Answers

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.

like image 150
kan Avatar answered Nov 15 '22 04:11

kan


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
like image 41
user4815162342 Avatar answered Nov 15 '22 04:11

user4815162342