Skip to content

Instantly share code, notes, and snippets.

@hiranp
Last active October 25, 2023 22:11
Show Gist options
  • Save hiranp/4c5440ba0d82693962fee897c36669b6 to your computer and use it in GitHub Desktop.
Save hiranp/4c5440ba0d82693962fee897c36669b6 to your computer and use it in GitHub Desktop.
Purging sensitive files from GIT
#!/bin/bash env
# Purge sensitive files from a git repo
# Based on https://help.github.com/articles/remove-sensitive-data/
# Better option: https://rtyley.github.io/bfg-repo-cleaner/
# Get current repo from .git/config
CURRENT_REPO=$(git config --get remote.origin.url)
echo $CURRENT_REPO
CURRENT_REPO_NAME=$(basename $CURRENT_REPO)
echo $CURRENT_REPO_NAME
# List of files to purge
purge_files=(
"file-to-purge.txt",
"another-file-to-purge.txt"
)
# Create temp repo clone
mkdir -p ../${CURRENT_REPO_NAME}-temp
# Check out the remote repo
git clone $CURRENT_REPO ../${CURRENT_REPO_NAME}-temp
cd ../${CURRENT_REPO_NAME}-temp
# Purge the files
for file in "${purge_files[@]}"; do
echo "Purging $file"
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch $file" --prune empty --tag-name-filter cat -- --all
# Make sure you don't accidentally commit the file again
echo file-to-purge.txt >>.gitignore
git add .gitignore
done
git commit -m "Prevent accidentally committing this again" .gitignore
# Push the edited repo. This will break other people's clones, if any.
git push origin master --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment