git錯誤:對象文件爲空(object-file-is-empty)解決方案

背景介紹

在平常開發過程當中咱們可能由於操做不當致使git版本庫出問題,最常常出的一個問題就是對象文件爲空錯誤object-file-is-empty,通常提示以下所示:html

error: object file .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0 is empty
fatal: loose object 3165329bb680e30595f242b7c4d8406ca63eeab0 (stored in .git/objects/31/65329bb680e30595f242b7c4d8406ca63eeab0) is corrupt

解決方案

  1. git init重頭開始,最暴力最直接解決方案,可是這樣以前的commit就沒有了,短時間的還好,長期的項目提交全不見了,貌似不太好git

  2. git clone/fetch克隆或拉取其餘人的項目,這樣能夠回溯到那我的項目的最新提交記錄,能夠挽救一些,萬一你的項目比他新好多呢ubuntu

總之,以上都不是最優的解決方案,最優的解決方案,就是咱們能夠恢復本機項目本來的git log記錄,修一下壞的節點就好app

最優解決方案

1.運行 git fsck --full

luyi@ubuntu:~/projects/example$ git fsck --full
error: object file .git/objects/3a/60046cdd45cf3e943d1294b3cb251a63fb9552 is empty
error: object file .git/objects/3a/60046cdd45cf3e943d1294b3cb251a63fb9552 is empty
fatal: loose object 3a60046cdd45cf3e943d1294b3cb251a63fb9552 (stored in .git/objects/3a/60046cdd45cf3e943d1294b3cb251a63fb9552) is corrupt

2. 選中空文件,刪除 rm filepath

luyi@ubuntu:~/projects/example$ rm .git/objects/3a/60046cdd45cf3e943d1294b3cb251a63fb9552
rm: remove write-protected regular empty file ‘.git/objects/3a/60046cdd45cf3e943d1294b3cb251a63fb9552’? y

3.再次運行 git fsck --full 通常會檢查到另一個新的空文件,再刪之。一勞永逸的辦法就是在.git目錄下find . -type f -empty -delete -print 會刪除所有空文件

luyi@ubuntu:~/projects/example/.git$ find . -type f -empty -delete -print
./objects/c6/492f7ad72197e2fb247dcb7d9215035acdca7f
./objects/9f/fe81f4bb7367c00539a9a0d366ff2a7558bfed
./objects/7c/b9a39246389a8417006b9365f6136823034110
./objects/de/1092e8c90cb025e99501a4be0035753b259572
./objects/6f/a8c9109f2e0d29095490ca8f4eaf024a6e2dcb

4.至此,全部空文件刪除完畢,再運行 git fsck --full,仍是有錯,head指向元素不存在,是以前一個空文件,咱們已經刪了。腫麼辦呢?

luyi@ubuntu:~/projects/example/.git$ git fsck --full
Checking object directories: 100% (256/256), done.
Checking objects: 100% (103719/103719), done.
error: HEAD: invalid sha1 pointer c6492f7ad72197e2fb247dcb7d9215035acdca7f
error: refs/heads/ia does not point to a valid object!
dangling blob 2a12b47881cd879987df8e50c9d900847c025bf4
dangling blob 5d4a7c616f0e59dd82608979e85d56f7a6785884
dangling blob cd6facde8c37f8389c12f3742185174a0536197b
dangling blob c2d0c0fba535d89cca9633d1b0ab780c8199dfd9
dangling blob 5117513970702f484278fd87732135a39588a073
dangling blob ac45f1874a0e42a6b9586594179f9825f3a40fd0
dangling blob c3355ad882b378740822e673d304e2cc091f1412
dangling blob 4d64dedf9f3518f25f7b7e3b1ad0dd6b477e0ce3
dangling blob 2487eae5cc4bfc23bc896c4051cb8f5522664e33
dangling blob 2a1907848af8aea53cb84e392225a5b6c4a81a5b
dangling blob 114233d9981a7933eacbff45a2b106ef1b61cfa9
dangling blob 9879ef3dd84a5515719fa7f35f982ac4d6a34e37
dangling blob f5b3bfd49e04c18ad12d590450a306a109b9151c
dangling blob 5fdb9f048aead1c85474fbca29b2ee327c821680

5.手動得到最後兩條reflog ,運行tail -n 2 .git/logs/refs/heads/ia

luyi@ubuntu:~/projects/example$ tail -n 2 .git/logs/refs/heads/ia
3a0ecb6511eb0815eb49a0939073ee8ac8674f8a 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5 盧禕 <luy@jjmmw.com> 1477039998 +0800    commit: nested into app

6.head當前是指向最新的那一條記錄,因此咱們看一下parent commit 即倒數第二條提交 git show 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5

luyi@ubuntu:~/projects/example$ git show 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5commit 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5
Date:   Fri Oct 21 16:53:18 2016 +0800

    nested into app

diff --git a/pzbm/templates/pzbm/base.html b/pzbm/templates/pzbm/base.html
index 70331c5..c96d12c 100644
--- a/pzbm/templates/pzbm/base.html
+++ b/pzbm/templates/pzbm/base.html
@@ -6,7 +6,7 @@

咱們能夠看到內容,是好着的fetch

7.那麼咱們就從新設置head,使其指向倒數第二條 git update-ref HEAD 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5

luyi@ubuntu:~/projects/example$ git update-ref HEAD 99cb711e331e1a2f9b0d2b1d27b3cdff8bbe0ba5
luyi@ubuntu:~/projects/example$ git s
On branch ia
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   pzbm/templates/pzbm/invest_advisor/reports.html

8.最後提交如下新代碼就OK了。這樣保留下來咱們的git log記錄,只是修一下壞的節點而已。

參考連接

翻譯自how-to-fix-git-error-object-file-is-empty翻譯

相關文章
相關標籤/搜索