通常,您不需要删除分支,您可以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