我运行了git update-ref -d HEAD命令,它删除了我所有的文件。文件夹大小仍然很大,所以,我想文件以某种方式在 git 的内存中。我怎样才能恢复这些文件?
这是打破你的回购协议的好方法;
$ mkdir DeleteHead
$ cd DeleteHead/
$ git init
$ touch README.md
$ git add -A
$ git commit -m "Init."
$ git commit -m "More work" --allow-empty
$ git log
commit 83edc8bcb2e627420e47b7933580dc85b21d968e (HEAD -> master)
Date: Fri Sep 14 17:42:47 2018 +0100
More work
commit c175c1fbf9ee8b9d2a9eec3eb3666f19db2b9f99
Date: Fri Sep 14 17:42:38 2018 +0100
Init.
$ git update-ref -d HEAD
Run Code Online (Sandbox Code Playgroud)
您无法使用通常的修复技巧;
$ git log
fatal: your current branch 'master' does not have any commits yet
$ git reflog
fatal: your current branch 'master' does not have any commits yet
Run Code Online (Sandbox Code Playgroud)
让我们在.git文件夹中查看一下,看看我们能找到什么;
$ cat .git/logs/HEAD
0000000000000000000000000000000000000000 c175c1fbf9ee8b9d2a9eec3eb3666f19db2b9f99 Horba 1536943358 +0100 commit (initial): Init.
c175c1fbf9ee8b9d2a9eec3eb3666f19db2b9f99 83edc8bcb2e627420e47b7933580dc85b21d968e Horba 1536943367 +0100 commit: More work
83edc8bcb2e627420e47b7933580dc85b21d968e 0000000000000000000000000000000000000000 Horba 1536943378 +0100
Run Code Online (Sandbox Code Playgroud)
这看起来很方便。让我们获取该83edc8bcb2e627420e47b7933580dc85b21d968e值(它在您的仓库中会有所不同)并将 HEAD 更新为该值;
$ git update-ref HEAD 83edc8bcb2e627420e47b7933580dc85b21d968e
$ git log
commit 83edc8bcb2e627420e47b7933580dc85b21d968e (HEAD -> master)
Date: Fri Sep 14 17:42:47 2018 +0100
More work
commit c175c1fbf9ee8b9d2a9eec3eb3666f19db2b9f99
Date: Fri Sep 14 17:42:38 2018 +0100
Init.
Run Code Online (Sandbox Code Playgroud)
现在一切都好。