うっかりセンシティブな情報をリポジトリに入れてしまって、あとで気づいて過去もろとも消したい場合にどうしたらいいか手順を解説します。
1. tigとかgit logで消したいコミットを探してリストアップする
$ git log --pretty=oneline --abbrev-commit
7217311 やばい <-- 消したい奴
15456c2 だいじょぶ
169d5d2 だいじょぶ
4a398a0 だいじょぶ
2. git rebaseで消したいコミットまで遡る
$ git rebase -i 7217311~1
pick 7217311 やばい <ーーこの行を消す
pick fe0a9fc XXXXXXXX
pick 9e1ca25 XXXXXXXX
pick ed6e651 XXXXXXXX
pick 2d53f34 XXXXXXXX
pick 4a398a0 XXXXXXXX
# Rebase f40d7bd..cb26656 onto f40d7bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
保存する
3. git rebase --skip でコミットを飛ばす OR 消したいファイルを削除して git add && git rebase --continue
1) コミットに含まれる一部のファイルだけなかったコトにしたい
rm -f 消したいファイル
git add -A
git rebase --continue
2) コミットに含まれるファイルすべてをなかったコトにしたい
git rebase --skip
あとは、ずっと git rebase --contitnue をやっていくだけ。
番外: やっぱ過去改ざんやめよう(ロールバック)
git rebase --abort