git filter-repo: remove sensitive files from Git history
A wp-config.php with production credentials, an .env file or a private key committed by accident does not go away with a new commit that deletes it: the file stays in every older commit, and anyone who clones the repository can get it. Removing it means rewriting the history. git filter-repo is the tool the Git project recommends for this instead of the old git filter-branch: much faster and much harder to misuse.
Problem: API keys, a database password or a large build archive were committed to a WordPress plugin repository. The files have been deleted, but they are still in the history.
Solution: First rotate the leaked secrets. Then rewrite the history in a fresh clone with git filter-repo (remove the files or replace the secret strings), check that nothing is left, force-push, and have everyone re-clone.
0. Rotate the secrets first
Once a secret has been pushed, assume it is known: forks, local clones, CI caches and mirrors all have it, and a history rewrite does not reach them. Change the database password, revoke and reissue the API keys, generate new salts for wp-config.php. The rewrite is the cleanup, not the fix.
1. Install and clone
pip3 install git-filter-repo # or: brew install git-filter-repo # Work in a fresh clone: filter-repo changes every commit ID git clone --no-local /path/to/repo /tmp/clean-repo cd /tmp/clean-repo
git filter-repo refuses to run in a repository that is not a fresh clone, unless you add --force: this protects your working copy from accidental rewrites.
2. Remove files from all commits
# One file git filter-repo --path wp-config.php --invert-paths # Several files and a folder git filter-repo --path .env --path wp-config.php --path config/secrets/ --invert-paths # By pattern git filter-repo --path-glob '*.pem' --invert-paths
--path selects what to keep; --invert-paths turns it into “remove these”.
3. Replace a secret inside files
When the secret is a line in a file that must stay (a hardcoded key in a PHP file), replace the string in all versions:
cat > /tmp/replacements.txt <<'TXT' sk_live_51Hx2EXAMPLE==>REMOVED literal:db_password_here==>REMOVED TXT git filter-repo --replace-text /tmp/replacements.txt
Each line is old==>new; without ==> the string is replaced with ***REMOVED***. Prefixes regex: and glob: allow patterns.
4. Check
git log --all --oneline -- wp-config.php # must print nothing git grep -n 'sk_live_51Hx2EXAMPLE' $(git rev-list --all) # must print nothing
5. Push and re-clone
filter-repo removes the origin remote on purpose, so you cannot push by accident. Add it back and force-push all branches and tags:
git remote add origin git@github.com:org/repo.git git push origin --force --all git push origin --force --tags
Everyone must delete their old clone and clone again: a git pull on top of the old history brings the removed commits back. Open pull requests point to old commit IDs and have to be recreated. On GitHub, old commits can still be reachable through cached views and forks for a while; GitHub support can purge them, and the secret scanning alerts show what is still exposed.
Bonus: split a plugin out of a monorepo
The same tool moves a folder into its own repository with its history:
git clone --no-local /path/to/monorepo /tmp/my-plugin cd /tmp/my-plugin git filter-repo --subdirectory-filter wp-content/plugins/my-plugin
The folder becomes the root of the repository, and only the commits that touched it remain.
NOTE: To prevent the next leak, keep secrets out of the repository from the start: .env and a local wp-config.php in .gitignore, a wp-config-sample.php with placeholders in Git, and a pre-commit secret scanner such as gitleaks.