8

我正在尝试一些githooks,所以我设置了一个本地裸存储库,其中包含一个hooks指向存储在其他地方的钩子的符号链接。

master我已将分支推送到gitrepo,当然,钩子失败了。:)

我想将gitrepo 重置为空,而不删除它并且必须重新创建符号链接等。

如何删除主分支,因为它是存储库中唯一的分支?

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

2 回答 2

18

要删除masterref,请使用git update-ref -d refs/heads/master.

你为什么不应该rm /refs/heads/master

packed-refs可能存在,因此rm有时无法按预期工作。

顺便说一句,如果您可以创建一个新的空仓库,那么重置的意义何在?只需使用git init --bare repo.git.

于 2012-09-03T10:42:17.797 回答
1

通常,您不需要删除分支,您可以git reset --hard REV将其设置为所需的新版本。但是,如果我对 yuo 的理解正确,您希望将其重置为“无”,即重置git init为第一次调用后的状态。git 似乎不允许你这样做,但你可以通过简单地删除.git/heads/refs/master. 这是一个新创建的 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

在这里你想这样做,但 git 不允许这样做:

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

但是,您可以这样做:

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

通过提交某些东西来检查它是否有效

[~/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
于 2012-09-03T10:44:51.633 回答