Skip to content

Instantly share code, notes, and snippets.

@arawako
Last active July 22, 2020 03:07
Show Gist options
  • Save arawako/7f6b849d016efdafcf8bea15e360d1f4 to your computer and use it in GitHub Desktop.
Save arawako/7f6b849d016efdafcf8bea15e360d1f4 to your computer and use it in GitHub Desktop.
A script that using git filter-branch for cleaning and not tracking sensible data in a repository of etckeeper or any git repository
#!/bin/bash
origin=/etc
tmp=$(mktemp -d)
cp -a $origin/. $tmp
cd $tmp
linestoremove=$(sed -n '/# BEGIN section managed by ansible/,/# END section managed by ansible/p' .gitignore | sed -e '/# BEGIN section managed by ansible/d' -e '/# END section managed by ansible/d' -e 's_^/__' | xargs)
filter=$(git filter-branch -f --index-filter 2>&1 1>/dev/null "git rm -r --cached --ignore-unmatch $linestoremove" --prune-empty -- --all)
if [[ $filter != "" ]]
then
exit -1
else
git reflog expire --expire=now --all
git gc --prune=now --aggressive
cp -a $origin/.git ~/.git.$(date +%Y%m%d%H%M%S)
rm -rf $origin/.git
cp -a .git $origin
rm -rf $tmp
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment