當 git 資料庫因為硬碟損壞導致 object 資料遺失時, 可參以下修復方式
(如果是 server side fail, 可登入到 bare repository 執行這些指令)git fsck 找出錯誤檔案
$ git fsck Checking object directories: 100% (256/256), done.從上面訊息可知, 有檔案內容遺失了
Checking objects: 100% (71975/71975), done.
broken link from commit 9ca0498a3497f4cfa3522664e37990ba0e7a8d27
to commit 448ab8ad722d8921824cad8a2d80c81d40338394
broken link from tree ccdf43a31989d487d2258e49e68ba1d6a841df7e
to tree 14032aabd85b43a058cfc7025dd4fa9dd325ea97
broken link from tree e975cd899efa1c2dc4386034c4601754dfc2a5a2
to blob ed3905e0e0c91d4ed7d8aa14412dffeb038745ff
dangling blob 1413fc609ab6f21774de0cb7e01360095584f65b
dangling commit d052bca582b3d08b2b309653b67b1e99cb835c9c
dangling blob 9e612858f802245ddcbf59788a0db942224bab35
missing commit 448ab8ad722d8921824cad8a2d80c81d40338394
dangling blob 94fb5490a2ed10b2c69a4a567a4fd2e4f706d841
missing blob ed3905e0e0c91d4ed7d8aa14412dffeb038745ff
dangling blob b93a4953fff68df523aa7656497ee339d6026d64
missing tree 14032aabd85b43a058cfc7025dd4fa9dd325ea97
dangling blob 64539b54c3751a6d9adb44c8e3a45ba5a73b77f0
這時候可從還正常的資料夾, 用以下指令還原遺失的 blob object
1. git cat-file -p ed3905e0e0c91d4ed7d8aa14412dffeb038745ff > /tmp/file.tmp
到有問題的資料夾中, 恢復遺失的 blob
2. git hash-object -w /tmp/file.tmp
如果有 obj files 消失, 可從正常的 repo 中取回
1. 把 pack 過的檔案移出 .git/objects/pack/
2. 解壓縮 pack 的檔案
# git unpack-objects < pack-xxxx.pack
參考資料:
Git-內部原理-Git-物件