The .env file containing secrets has been removed from the current commit, but it still exists in git history. Anyone with access to the repository can retrieve past commits and extract the secrets.
Before cleaning git history, rotate these secrets immediately:
# Database password
# Redis password
# MQTT passwords
# JWT secrets (if any were set)# Install BFG
brew install bfg # macOS
# or download from https://rtyley.github.io/bfg-repo-cleaner/
# Backup your repo first
cd /Users/kpres12/Downloads/Sentinel
git clone --mirror . ../Sentinel-backup.git
# Remove .env from all commits
bfg --delete-files .env
# Clean up
git reflog expire --expire=now --all
git gc --prune=now --aggressive
# Force push (WARNING: this rewrites history)
git push origin --force --all
git push origin --force --tags# Install
pip install git-filter-repo
# Remove .env from history
git filter-repo --path .env --invert-paths
# Force push
git push origin --force --allgit filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push origin --force --all
git push origin --force --tagsAfter cleaning history, all team members must:
# Delete local repo
cd ~/path/to/Sentinel
cd ..
rm -rf Sentinel
# Re-clone
git clone <repo-url>
cd SentinelDo NOT use git pull – it will re-introduce the old history.
- Copy
.env.production.templateto.env.production - Generate new secrets:
# Generate a secret openssl rand -hex 32 # Or for base64 openssl rand -base64 32
- Update secrets in your secrets manager (AWS Secrets Manager, Vault, etc.)
- Redeploy with new secrets
- Require pull request reviews
- Enable status checks
- Prevent force pushes to main/master (after cleanup)
- Enable secret scanning alerts
-
.envremoved from tracking -
.gitignoreupdated to block all.env*files -
.env.production.templatecreated with no secrets - Git history cleaned (choose method above)
- All secrets rotated
- Team notified to re-clone
- Branch protection enabled
- Secret scanning enabled (GitHub Advanced Security)
Never commit files containing:
- Passwords
- API keys
- Private keys
- Certificates
- Tokens
- Any credential or secret
Always use:
.env.templatefiles with placeholder values- Environment variables at runtime
- Secrets managers (AWS Secrets Manager, HashiCorp Vault, etc.)
- Git hooks to prevent secret commits (see
.git/hooks/pre-commit.sample)