LoginSignup
32
28

More than 5 years have passed since last update.

BFGを使ってgitリポジトリから巨大なファイルを削除してみた

Last updated at Posted at 2016-01-23

BFGを使うべき状況

  • gitに機密情報(パスワード、AWSのキー、秘密鍵)をコミット、pushしてしまったのでリポジトリから完全に削除したいとき
  • 巨大なファイルを削除してリポジトリを軽量化したいとき
  • git filter-branchのコマンドは複雑で間違えそう

特徴

  • Scalaで書かれているのでJavaが必要
  • gitより速い
  • 使い方がシンプル

BFG公式サイト
https://rtyley.github.io/bfg-repo-cleaner/
githubの公式ページでもBFGを使う方法が紹介されている。
https://help.github.com/articles/removing-files-from-a-repository-s-history/

使い方は基本的にBFG公式サイトの「Usage」に書いてある通りだが、試しに一通りやってみた。以下はその記録。

やってみた記録

まずBFGをダウンロードして、シェルでaliasを設定する。

wget http://repo1.maven.org/maven2/com/madgag/bfg/1.12.8/bfg-1.12.8.jar
alias bfg='java -jar bfg-1.12.8.jar'

テスト用リポジトリをcloneしてくる。このとき--mirrorをつける。

git clone --mirror git@github.com:aosho235/gittest.git
cd gittest

試しに大きなファイル(bfg-1.12.8.jar 14MB)をコミットしてみる。

cp ../bfg-1.12.8.jar .
git add bfg-1.12.8.jar
git commit -m "add bfg"

BFGを実行してみる(10MBより大きなファイルを削除)。

cd ..
bfg --strip-blobs-bigger-than 10M gittest
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Using repo : /home/aoyama/gittest/.git

Scanning packfile for large blobs: 1Scanning packfile for large blobs: 20
Scanning packfile for large blobs completed in 23 ms.
Warning : no large blobs matching criteria found in packfiles - does the repo need to be packed?
Please specify tasks for The BFG :
bfg 1.12.8
Usage: bfg [options] [<repo>]

  -b <size> | --strip-blobs-bigger-than <size>
        strip blobs bigger than X (eg '128K', '1M', etc)
  -B NUM | --strip-biggest-blobs NUM
        strip the top NUM biggest blobs
...

BFGは安全のためデフォルトではHEADに変更を加えないという仕様なので、何も削除されなかった(と思ったが、単に後述のようにpackされてないから削除されなかったのかも)。

ではjarを削除してコミットしてみる。

cd gittest
git rm bfg-1.12.8.jar
git commit -m "remove"

もう一度BFGを実行する。

cd ..
bfg --strip-blobs-bigger-than 10M gittest

さっきと同じメッセージが出る。まだ削除されないようだ。
「does the repo need to be packed?」と言っているので、git gcしてみる。

cd gittest
git gc

もう一度BFGを実行する。

cd ..
bfg --strip-blobs-bigger-than 10M gittest
...
Deleted files
-------------

    Filename         Git id
    -----------------------------------
    bfg-1.12.8.jar | abbfd6bd (13.9 MB)


In total, 3 object ids were changed. Full details are logged here:

    /home/aoyama/gittest.bfg-report/2016-01-23/18-52-18

BFG run is complete! When ready, run: git reflog expire --expire=now --all && git gc --prune=now --aggressive
...

今度は削除された。
「BFG run is complete! When ready, run: git reflog expire --expire=now --all && git gc --prune=now --aggressive」
というメッセージが出ている。
この時点ではdu -hしてもまだディスク使用量は減っていない。
言われたとおりのコマンドを実行してみる。

git reflog expire --expire=now --all && git gc --prune=now --aggressive

完全に削除され、du -hでディスク使用量が減っていることが確認できた。

最後にgithubにpushする。

git push

cloneしたときに--mirrorをつけていたので、このpushで全てのrefがpushされるらしい。
全ての開発者にcloneし直してもらう。

32
28
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
32
28