読者です 読者をやめる 読者になる 読者になる

「error: object file .git/objects/~ is empty」の対処法

いろいろな方が書いているけど、自分用に書いておかないと忘れちゃうのでメモ

経緯

いつもどおり、作業開始前に「git pull」したら以下のエラーが表示された。

% git pull
error: object file .git/objects/f6/fac924ccdbc54addeadac5149bd3c45a45d7ad is empty
fatal: loose object f6fac924ccdbc54addeadac5149bd3c45a45d7ad (stored in .git/objects/f6/fac924ccdbc54addeadac5149bd3c45a45d7ad) is corrupt

状況としては、昨日は仕事場のPCで作業したあとにgit push、家に帰って追加作業した git push、で、今日、仕事場で上記のエラー。つまり、リモートリポジトリが最新状態で、ローカルリポジトリの方が古い状態。なので、リモートリポジトリからpullできるようにするのが今回の目標

対処法

日本語のページもいろいろあったのだけど結局以下のページのとおりに進めたら解決した。

Gitで管理しているディレクトリーを ~/WorkDirとする。まず、~/WorkDir/.gitをコピーして保存。

% cd ~/WorkDir
% cp -pr .git .git-old

続いて fsck を実行。

% git fsck --full
Checking object directories: 100% (256/256), done.
error: object file .git/objects/d5/eec8592ee4d8fb6a5e6293e3bcf8611c024882 is empty
fatal: loose object d5eec8592ee4d8fb6a5e6293e3bcf8611c024882 (stored in .git/objects/d5/eec8592ee4d8fb6a5e6293e3bcf8611c024882) is corrupt

emptyなobjectファイルを削除する。findコマンドを使ってまとめて消すことができる。

%  find . -type f -empty -delete -print
./FETCH_HEAD
./objects/f6/fac924ccdbc54addeadac5149bd3c45a45d7ad
./objects/d5/eec8592ee4d8fb6a5e6293e3bcf8611c024882

再び fsck。他のエラーはあるけどemptyファイルに由来するエラーはなくなった。

% git fsck --full
Checking object directories: 100% (256/256), done.
error: .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack SHA1 checksum mismatch
error: index CRC mismatch for object 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: cannot unpack 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: failed to read delta base object 8b06f3e6621e49ae6feefc517bd808fdf90d926b at offset 64243959 from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack
error: cannot unpack 19b3bbceea4399c0c2d878701a846e9a4211f45e from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 69361275
Checking objects: 100% (76960/76960), done.
error: HEAD: invalid sha1 pointer f6fac924ccdbc54addeadac5149bd3c45a45d7ad
error: refs/heads/master does not point to a valid object!
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
notice: No default references
error: d5eec8592ee4d8fb6a5e6293e3bcf8611c024882: invalid sha1 pointer in cache-tree
missing blob 8b06f3e6621e49ae6feefc517bd808fdf90d926b

「error: HEAD: invalid sha1 pointer f6fac924ccdbc54addeadac5149bd3c45a45d7ad」HEADで削除したコミットを参照している。エラーがでることを確認する。

% git reflog
fatal: bad object HEAD

「f6fac924ccdbc54addeadac5149bd3c45a45d7ad」の直前のコミットを探す。

% tail -n 2 .git/logs/refs/heads/master 
97c909ae5ec753483681864415646bec2375bf33 950b6e5b37e239dd8948b5e23807701dbd7abcea next49 <next49@dummy.com> 1477839127 +0900	commit: Dummy message1
950b6e5b37e239dd8948b5e23807701dbd7abcea f6fac924ccdbc54addeadac5149bd3c45a45d7ad next49 <next49@dummy.com> 1477839162 +0900	pull: Dummy message2

2行目の最初のハッシュ値が直前のコミットを指すもの。コミットの内容を確かめる。

% git show 950b6e5b37e239dd8948b5e23807701dbd7abcea
~内容略~

実際に存在するコミットなので、HEADをこのコミットに置き換える。

% git update-ref HEAD 950b6e5b37e239dd8948b5e23807701dbd7abcea

もう一度 fsck

% git fsck --full
Checking object directories: 100% (256/256), done.
error: .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack SHA1 checksum mismatch
error: index CRC mismatch for object 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: cannot unpack 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: failed to read delta base object 8b06f3e6621e49ae6feefc517bd808fdf90d926b at offset 64243959 from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack
error: cannot unpack 19b3bbceea4399c0c2d878701a846e9a4211f45e from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 69361275
Checking objects: 100% (76960/76960), done.
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: d5eec8592ee4d8fb6a5e6293e3bcf8611c024882: invalid sha1 pointer in cache-tree
missing blob 8b06f3e6621e49ae6feefc517bd808fdf90d926b

「error: d5eec8592ee4d8fb6a5e6293e3bcf8611c024882: invalid sha1 pointer in cache-tree」は削除したコミットに由来するものと思われる。確認する。

% git status
~内容略~

現在ステージ上の変更(git add の後)を削除する。

% rm .git/index
% git reset

また、fsck

% git fsck --full
Checking object directories: 100% (256/256), done.
error: .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack SHA1 checksum mismatch
error: index CRC mismatch for object 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: cannot unpack 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: failed to read delta base object 8b06f3e6621e49ae6feefc517bd808fdf90d926b at offset 64243959 from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack
error: cannot unpack 19b3bbceea4399c0c2d878701a846e9a4211f45e from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 69361275
Checking objects: 100% (76960/76960), done.
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
missing blob 8b06f3e6621e49ae6feefc517bd808fdf90d926b

さきほどの「invalid sha1 pointer in cache-tree」というエラーが消えているのがわかる。

念のため、gc を実行。

% git gc
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
warning: reflog of 'refs/heads/master' references pruned commits
warning: reflog of 'refs/remotes/origin/master' references pruned commits
warning: reflog of 'HEAD' references pruned commits
Counting objects: 77162, done.
Delta compression using up to 2 threads.
error: bad packed object CRC for 8b06f3e6621e49ae6feefc517bd808fdf90d926b
error: failed to read object 8b06f3e6621e49ae6feefc517bd808fdf90d926b at offset 64243959 from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack
fatal: packed object 8b06f3e6621e49ae6feefc517bd808fdf90d926b (stored in .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack) is corrupt
error: failed to run repack

ここまでで対応終了。参考元だとcommitしているのだけど、私の目的はリモートリポジトリからのpullなのでコミットしない。

pull してみる。

% git pull
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
Enter passphrase for key '/home/next49/.ssh/id_rsa': 
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
error: refs/remotes/origin/HEAD does not point to a valid object!
error: refs/remotes/origin/master does not point to a valid object!
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 22 (delta 14), reused 0 (delta 0)
Unpacking objects: 100% (22/22), done.
From ssh://192.168.11.1/home/next49/workfiles
 * [new branch]      master     -> origin/master
Updating 950b6e5..21c20e2
error: Your local changes to the following files would be overwritten by merge:
	/path/to/hogehoge.pptx
Please, commit your changes or stash them before you can merge.
Aborting

バイナリーファイルがコンフリクトしている。これは git reset した影響。リモートリポジトリを優先する。

% git checkout --theirs /path/to/hogehoge.pptx

再びpullすると無事に成功。念のため、もう一度fsck

% git fsck --full
Checking object directories: 100% (256/256), done.
error: .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack SHA1 checksum mismatch
error: index CRC mismatch for object 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: cannot unpack 8b06f3e6621e49ae6feefc517bd808fdf90d926b from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 64243959
error: inflate: data stream error (invalid stored block lengths)
error: failed to read delta base object 8b06f3e6621e49ae6feefc517bd808fdf90d926b at offset 64243959 from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack
error: cannot unpack 19b3bbceea4399c0c2d878701a846e9a4211f45e from .git/objects/pack/pack-55bbe2351e8cd70def9bc91271a9d66be731ec66.pack at offset 69361275
Checking objects: 100% (76960/76960), done.
missing blob 8b06f3e6621e49ae6feefc517bd808fdf90d926b

相変わらずエラーはでているけれども「~ does not point to a valid object!」というエラーは消えた。以上で無事解決。