0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Git】.git/objectsが破損してリポジトリが壊れたときの復旧手順

Posted at

はじめに

こんにちは。アメリカ在住で独学エンジニアを目指している Taira です。

ある日、git status を実行したらこんなエラーが出ました

error: object file .git/objects/74/224a36af0929241ca49028e4c82ba081502c2a is empty
fatal: loose object 74224a36af0929241ca49028e4c82ba081502c2a (stored in .git/objects/74/224a36af0929241ca49028e4c82ba081502c2a) is corrupt

これは .git/objects 配下の Git オブジェクトが破損してしまったことを意味しています。
つまり、ローカルリポジトリ自体が壊れている状態です。

この記事では、安全にリポジトリを復旧する手順を紹介します。


症状の例

  • git statusgit log が動かない
  • fatal: loose object ... is corrupt が出る
  • .git/objects 配下の一部ファイルが空(サイズ0)になっている

結論:再クローンが最も安全

.git フォルダが破損している場合、修復コマンド(git fsck, git prune など)では直らないことが多いです。
リモートに最新があるなら、再クローンするのが最も確実で安全です。


復旧手順

1. まずはリモートのURLを確認

壊れたリポジトリがどこの GitHub に紐づいているか確認します。

git config --get remote.origin.url

出力例:

https://github.com/username/your-repo.git

このURLを控えておきます。


2. 壊れたリポジトリを退避

安全のため、今のフォルダをリネームしてバックアップを取っておきましょう。

cd ..
mv your-repo your-repo_broken_backup

3. リモートから再クローン

控えておいたURLを使って、再クローンします。

git clone https://github.com/username/your-repo.git your-repo

4. 環境変数・設定ファイルを戻す

clone では .envconfig/* などGit管理外のファイルは復元されません。
壊れたバックアップから必要なファイルをコピーして戻しましょう。

cp your-repo_broken_backup/.env your-repo/

(または他の設定ファイルも同様にコピー)


✅ まとめ

ステップ 内容
1 リモートURLを確認
2 壊れたリポジトリをリネームしてバックアップ
3 リモートから再クローン
4 .env など環境ファイルを復元

補足:なぜ壊れるのか?

主な原因は以下の通りです:

  • PCの強制シャットダウンやディスク書き込み中断
  • WSLや仮想環境でのファイルシステム競合
  • .git フォルダを外部ツールで同期(例: OneDrive, Dropbox)していた

特に Windows × WSL 環境では、リポジトリをCドライブではなくWSL内に置く方が安全です。
(→ I/Oエラーや速度低下も防げます)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?