Oteto Blogのロゴ

【Git】remote unpack failed:index-pack failedの対処法

困ったこと

remote: fatal: did not receive expected object xxx
error: remote unpack failed: index-pack failed
To https://github.com/xxx/xxx.git
 ! [remote rejected] main -> main (failed)
error: failed to push some refs to 'https://github.com/xxx/xxx.git'

GitHubで新規作成したリポジトリにローカルのプロジェクトを初回commit&pushしようとすると失敗した。

error: remote unpack failed: index-pack failedが原因のよう。

解決法

rm -rf .git
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/xxx/xxx.git
git push -u origin main

色々試行錯誤したが、結果的にローカルの.gitを削除しfirst commitをやり直すことで解決した。

そのプロジェクトは個人開発かつ初回のgit initから大して履歴が生まれていないため、この方法で問題ないと判断。

原因

調べたところアクセス権限周りの問題というものも見受けられたが、自分の場合はコミット履歴のファイル数やサイズが大きいことが原因のようだった。

というのもbuildして生成されるディレクトリ(いわゆるdist的なもの)を.gitignoreに記述しておらず、コミット履歴のサイズが肥大化していた。