#!/bin/bash
#https://help.github.com/articles/removing-sensitive-data-from-a-repository/

if [ ! -d ".git" ]; then
	>&2 echo "GitRmHist must be called from the root of the repository"
	exit 1
fi

if [[ $# -eq 0 ]] ; then
	echo "usage: gitrmhist path/to/file_or_dir"
	exit 2
fi

echo "Removing $1 from repository history..."
git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch $1" --prune-empty --tag-name-filter cat -- --all || exit $?

echo
echo "Pushing removals to remote..."
git push origin --force --all
#git push origin --force --tags


echo
echo "Remove cached objects in local repository..."
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

echo
echo "Done"
echo "Remember to add $1 to .gitignore"
echo "Remember to rebase (not merge) other clones of the repo with:"
echo "   git pull --rebase"
