LoginSignup
21
10

More than 1 year has passed since last update.

git-filter-repo

Posted at

ある日、まずいファイルをコミットしちゃったなーと気が付き歴史改変を試みた。「git 履歴 削除」などで検索すると git filter-branch を使うらしい。そこでこのコマンドのドキュメント https://git-scm.com/docs/git-filter-branch を読んでみたが、なんと面白い事に公式では git filter-branch ではなくサードパーティの git filter-repo https://github.com/newren/git-filter-repo/ を推奨していたので使ってみた。

Mac でのインストール

brew install git-filter-repo

使い方

安全のため、このツールは必ず clone したての新鮮なレポジトリで操作する。理由はマニュアルに載っているが、新鮮なレポジトリを使う事で、うっかりバックアップ無しで作業してしまったり、意図せざる内部の不整合が起こってしまう問題を回避する意図がある。

マニュアルに使い方の例があるが、例えば git の履歴から hoge.txt を完全に消すには

git filter-repo --path hoge.txt --invert-paths

のようにする。--path がパスの指定で、--invert-paths が指定したパスを消す事を示す。

指定したパスだけ残してあとの全部を消したい場合は --invert-paths を付けなければ良い。

コマンドを実行したあとは、安全のためにリモートオリジンの設定が消える。そこでもしもリモートも改変したい場合は

git remote add origin ssh://git@git.example.com/example.git
git push -f origin master

のようにする。この作業を滅多なことで行うべきでは無いので、それなりに面倒になっている。

参考

21
10
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
21
10