Gitで'fatal: object {hash} is corrupted'になった際の復旧手順
Macがフリーズする現象がたまにあって、そうなるとgitの状態が壊れることがある。
その時の修復手順を記録として残しておく。
現象
git操作を行おうとするとfatal: object <hash> is corrupted
が出る。
/home/kanno/repository% git status fatal: object 43a1c7ca8e29251954a806714bd1db24ddcf1905 is corrupted
復旧手順
念のためバックアップを取っておく
/home/kanno/repository% cp -rp .git git-old
表示されたhashのオブジェクトファイルを消す
/home/kanno/repository% rm .git/objects/04/313fa358eabb459b0c9ea1f48709f1cbb88118
git操作でfatal: bad object HEAD
が出るまでオブジェクトファイルを消すのを繰り返す
/home/kanno/repository% git status fatal: bad object HEAD
該当ブランチのrefs/headsの中身を見る
(.git/logs/refs/heads/20432-hoge-branch
のところは見たいブランチを指定)
/home/kanno/repository% tail -n 2 .git/logs/refs/heads/20432-hoge-branch dcba72adc7078cfcced387a69246f0b29ea589b6 74e8aa0c09550804b40024ab33a98321627e1b4a kannokanno <...> 1429095527 +0900 commit: [#20432] WIP 74e8aa0c09550804b40024ab33a98321627e1b4a 43a1c7ca8e29251954a806714bd1db24ddcf1905 kannokanno <...> 1429096095 +0900 commit: [#20432] WIP
2行目の左のhash値を使ってHEAD
を戻す
/home/kanno/repository% git update-ref HEAD 74e8aa0c09550804b40024ab33a98321627e1b4a
git操作が出来るようになっているのでcommitしなおしておく