ハードとソフトの中間点

Gitで歴史から不要ファイルを削除してリポジトリを軽くする

ログファイルなど不要なファイルを歴史から抹消して,リポジトリを軽くする.

カテゴリ: tips

参考: https://easyramble.com/git-filter-branch.html

歴史から削除

ブランチごとに,要らないファイルを消していく.

  • ファイルの場合
$ git filter-branch -f --index-filter 'git rm --ignore-unmatch [filename]' HEAD
  • ディレクトリの場合
$ git filter-branch -f --index-filter 'git rm -rf --ignore-unmatch dirname' HEAD

歴史の掃除

上記だけでは消えない.全ての歴史を抹消する.ただし,以下のコマンドは復元できないので,必ずバックアップ等をとるように.

$ git for-each-ref –format=’delete %(refname)’ refs/original | git update-ref –stdin
$ git reflog expire –expire=now –all
$ git gc –prune=now --aggressive

さらに掃除

実は,これでもまだ不十分.

きれいにしたリポジトリを一旦まったく新しいリモートに上げて,再びクローンする.新しいリモートはローカルなりなんなりに適当に立てる.

$ mkdir ~/tmp-git-repo.git
$ cd ~/tmp-git-repo.git
$ git init --bare
$ cd [path to the repository]
$ git remote add tmp ~/tmp-git-repo.git
$ git push -u tmp --all
$ mkdir ~/new-repo
$ cd ~/new-repo
$ git clone ~/tmp-git-repo.git

感想

初めから不要なファイルは入れないようにしましょう.