Skip to content

Instantly share code, notes, and snippets.

@agarny
Created May 8, 2013 15:05
Show Gist options
  • Save agarny/5541082 to your computer and use it in GitHub Desktop.
Save agarny/5541082 to your computer and use it in GitHub Desktop.
A simple shell script (based on information found at https://help.github.com/articles/remove-sensitive-data) to remove files based on their extension. The idea was for me to permanently remove some binary files I had in a git repository history.
#!/bin/sh
function updateFiles()
{
export files=`git rev-list --all --objects | awk '{ print $2; }' | sort | uniq | grep -e "\.dll$" -e "\.dll\.a$" -e "\.dylib$" -e "\.lib$" -e "\.pdf$" -e "\.pptx$" -e "\.so$" -e "\.so\..$" -e "\.zip$" | sort | uniq`
}
updateFiles
needCleaningUp=0
while [ "$files" != "" ]
do
# Note: from personal experience, the loop is needed to make sure that all
# files are actually removed. YMMV, but it certainly doesn't harm
# having it...
needCleaningUp=1
git filter-branch --force --index-filter 'git rm -f --cached --ignore-unmatch $files' --prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original
git reflog expire --expire=now --all
updateFiles
done
if [ $needCleaningUp -eq 1 ]
then
git gc --prune=now
git gc --aggressive --prune=now
fi
# Note: following the execution of this script, I force pushed everything back
# to my git repository using
#
# $ git push --force --all
#
# and then my tags using
#
# $ git push --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment