#背景
個人のHPをgitで管理しようと思い、commitしてgithubにpushしたらファイルのサイズが重くてエラーが出ました。そこで原因となったファイルを削除して再度pushしたところ同じようなエラーが出てしまいました。しかも原因のファイルはそもそも不要なファイル。このような問題が起きた場合の対処法について記事にしました。
#状況
- Homepage用のソースファイルを「git init」コマンドを入力し、gitの管理下に置く
- 「git add .」で全てのファイルやフォルダーをステージングする。
- 「git commit」で初コミットをする。
- github上にリポジトリーを作成する。
- 「git remote add origin {githubのリポジトリーURL}」を打ち、originという名前でgithubの
ブランチを追加する。 - 「git push origin master」でgithubのmasterブランチにpushする。
- 以下のようなエラーが発生
Enumerating objects: 98, done.
Counting objects: 100% (98/98), done.
Delta compression using up to 4 threads
Compressing objects: 100% (91/91), done.
Writing objects: 100% (98/98), 558.84 MiB | 1.04 MiB/s, done.
Total 98 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), done.
remote: warning: File xxxxxxx.pdf is 90.41 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: error: Trace: 5b95e6f45d3f8fba822fa66d345dae6ad53f56c312c8480a08044e2ec87d866e
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File xxxxx.pdf is 114.80 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: File xxxxx.pdf is 108.29 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: File xxxxx.pdf is 109.45 MB; this exceeds GitHub's file size limit of 100.00 MB
To https://github.com/xxxxxxx/homepage.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/xxxxxx/homepage.git'
#原因
githubにpushする時、ファイルが重かった。
--> this is larger than GitHub's recommended maximum file size of 50.00 MB
訳すとgithubにアップロードできる最大のサイズは50.00MBであるということ。
実はgithubって一度にpushできるサイズの上限があったらしい。
#解決策について
①履歴を確認し過去に戻してみる
1.「git log」で履歴を確認する。
2.「git reset --hard 履歴のid番号」を打ち、pushする前の履歴に戻す
3.原因となる不要なpdfファイルを削除 ->「remote: error: File xxxxx.pdf…」
4.git add & commit
5.再度「git push origin master」でgithubにpushする
6.結果、同じようなエラーが出る。
②履歴を戻しても一度commitしてしまったので、ステージングしたファイルは残っていると思い、revertコマンドで打ち消してみる
1.まずは元のフォルダーのコピーを作成
2.「git revert HEAD」と入力し、コミットを打ち消す。
3.ファイルがきれいに削除されていることを確認。
4.元のフォルダーのコピーから問題の「pdfファイル」と「.gitフォルダー」を除いたものをcopyし、きれいになった元のファイルにpasteする。
5.git add & commit
6.再度「git push origin master」でgithubにpushする
7.またして同じようなエラーが出る。
③元のフォルダーにある「.git」と原因の「pdfファイル」を削除
どうやら元のフォルダーにpdfファイルを削除する前の履歴が残っているのが原因だと思ったので、「.git」を削除。原因のpdfファイルも削除した上でgitの初期設定を行う。
1.「.git」フォルダーを削除。
2.原因のpdfファイルを削除。
3.「git init」で「.git」を新規作成。
4.「git remote add origin {githubのリポジトリーURL}」を打ち、originという名前でgithubの
ブランチを追加。
5.「git add.」でステージング、「git commit」でコミットする。
6.「git push origin master」でgithubにpushする
7.容量オーバーのエラーが出ず、意図通りにpush成功!
#終わりに
1.サイズオーバーしたファイルがcommitされた場合、resetやrevertをしても原因のファイルがpushされてしまったので、他の解決策を探してみますがアドバイスいただけるとうれしいです。
2.今回の場合、サイズオーバーしたファイルをgithubにpushするわけではなく、取り除いた状態でpushしgithubのリポジトリーに反映したかったという… 容量オーバーでもpushに失敗しない方法については以下の記事通り。