1. 程式人生 > >解決git錯誤: error object file is empty , The remote end hung up unexpectedly

解決git錯誤: error object file is empty , The remote end hung up unexpectedly

今天執行git pull時遇到麻煩,應該是昨天電腦異常關機造成的git資料損壞:

$git pull
error: object file .git/objects/88/526655aa4eca14ead2d443e80082276a79e0c2 is empty
error: object file .git/objects/88/526655aa4eca14ead2d443e80082276a79e0c2 is empty
fatal: loose object 88526655aa4eca14ead2d443e80082276a79e0c2 (stored in .git/objects/88/526655aa4eca14ead2d443e80082276a79e0c2) is corrupt
fatal: The remote end hung up unexpectedly

這種情況以前也偶爾遇到過,我當時的解決辦法又笨又簡單,就是刪除當前程式碼重新從遠端倉庫下載一份程式碼,反正只要確保遠端倉庫是最新完事的程式碼就沒問題。 這次不同了,本地有不少花時間生成的中間結果程式碼,刪除之後重建要花時間,所以就不想用這麼笨的辦法。 在stackoverflow上找到這篇文章《how to fix GIT error: object file is empty?》,幾個answer提供了不同的解決方式,第一種方法,好煩好複雜,看不下去,後來發現國內不少翻譯這篇文章的帖子,介紹的就是第一種方法。

而第二個answer提供的方法就簡單明白許多,過程就三步:

在這裡插入圖片描述

於是嘗試用這個辦法:

step1: 刪除所有因為資料損壞導致的空物件檔案(remove any empty object files)。執行第一步沒有任何輸出

$ find .git/objects/ -type f -empty | xargs rm

step2: 下載上一步刪除的物件檔案(fetch down the missing objects)

$ git fetch -p
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
remote: Counting objects: 118, done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 118 (delta 92), reused 0 (delta 0)
Receiving objects: 100% (118/118), 15.90 KiB | 0 bytes/s, done.
Resolving deltas: 100% (92/92), completed with 26 local objects.
From https://gitee.com/l0km/app
 * [new branch]      master     -> origin/master

step3: 做一次全面的儲存物件檢查(do a full object store check)

$ git fsck --full
Checking object directories: 100% (256/256), done.
Checking objects: 100% (6619/6619), done.
dangling commit 18086406efd2c3e37dc478744304dd2a758206bb
dangling blob 760bbd43e62054478eb3edbffd88ea65adb3f094
dangling commit fcc10548cc0d06de24b2903cf3ec745bb78185cb
dangling blob 234ff40df2a81d1a2625aae8ef76f6c7c92dc5f6
dangling blob 38e25f663bff5ebec04fa559500860e90fd4bf23
dangling commit 406f700c73b3a263604fea2a464634b389153835
dangling commit bcfcb27bfdb5d54b80557a9723bcbcab6b601a90

三步完成之後,再次執行git pull,錯誤消失,成功。

$ git pull
Updating 8852665..f66f1dd
Fast-forward
 CMakeLists.txt                                 |  4 +--
 dependencies/build_mgncs.sh                    |  1 +
 dependencies/cmake/Modules/FindLIBCONFIG.cmake |  4 ++-
 dependencies/cmake/Modules/FindMGNCS.cmake     |  2 +-
 dependencies/cmake/Modules/FindMGPLUS.cmake    |  2 +-
 dependencies/cmake/Modules/FindMGUTILS.cmake   |  2 +-
 dependencies/cmake/Modules/FindMINIGUI.cmake   |  2 +-
 dependencies/cmake/Modules/depcommon.cmake     | 22 +++++++-----
 dependencies/mips_build_mgncs.sh               | 47 +++++++++++++-------------
 facelock/CMakeLists.txt                        |  6 +++-
 10 files changed, 52 insertions(+), 40 deletions(-)