[Git] 間違えてcommitしたファイルをリポジトリから削除する

例えば、パスワードや個人情報が書かれたファイルを履歴も含めて削除する場合。
git rm –cached ではリポジトリに残っている履歴は消えない。

  • Remove sensitive data · GitHub Help
  • GitHubに上げてしまったパスワードを書いたファイルを消す方法 – Qiita [キータ]
    git filter-branch
    % git filter-branch –force –index-filter \
    ‘git rm –cached –ignore-unmatch AWDwRails32.sublime-workspace’ \
    –prune-empty –tag-name-filter cat — –all
    Rewrite 4d8403a6605b2ef7934817ad94a3bcd19bb1ee91 (8/39)rm ‘AWDwRails32.sublime-workspace’
    # 省略
    Rewrite 79a62e5753cf912b1ed790551c4db044b2903b6f (38/39)rm ‘AWDwRails32.sublime-workspace’
    Rewrite 8811c830be7d4ada9cf50af2c7e3f107e44e73e7 (39/39)
    Ref ‘refs/heads/depot_e_RPT’ was rewritten
    Ref ‘refs/heads/depot_f_RPT’ was rewritten
    Ref ‘refs/heads/depot_g_RPT’ was rewritten
    Ref ‘refs/heads/develop’ was rewritten
    Ref ‘refs/heads/master’ was rewritten
    Ref ‘refs/remotes/origin/master’ was rewritten
    force push します。
    % git push -f origin master
    Counting objects: 431, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (369/369), done.
    Writing objects: 100% (383/383), 362.71 KiB | 0 bytes/s, done.
    Total 383 (delta 185), reused 0 (delta 0)
    To git@github.com:DriftwoodJP/Agile-Web-Development-with-Rails.git

    • 8811c83…d0e3c9b master -> master (forced update)