Pro Tip: Scrub files from Git history

Thought I'd start doing quick tips alongside my regular tutorials to supplement your development and bring attention to some awesome tips and tricks you wouldn't otherwise have been aware of. This is a trick I came across recently when I accidentally pushed a sensitive key into my git remote. Crap! This is now in the history for anyone to view...Well luckily we can scrub any file from a git remote with the following commands

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE' \
--prune-empty --tag-name-filter cat -- --all

Be sure to replace PATH-TO-YOUR-FILE with the name of your file. You should see a similar result to this

Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
Ref refs/heads/master was rewritten

Finally we can now git push --force to our remote and the history of our sensitive file is now gone. Be sure to add your files to a .gitignore so it doesn't get checked in again!

For more information on scrubbing git history check this link: