Removing Specific Files
To remove a file containing sensitive data from all of history:1
Clone a fresh copy
2
Remove the file
--invert-paths flag removes the specified path instead of keeping it.3
Verify removal
4
Force push
Removing Multiple Files
For removing many files at once, create a file listing all paths to remove:1
Create the paths file
Create
../files-to-delete.txt with one path per line:2
Remove all listed files
Removing Files by Pattern
Remove all files matching a specific pattern:Replace Text in Files
For removing sensitive strings embedded in files (like API keys in code):1
Create a replacements file
Create
../replacements.txt with the format FIND==>REPLACE:You can also use regex patterns:
2
Apply replacements
Advanced: Using Callbacks
For complex removal scenarios, use Python callbacks:Remove specific blob by hash
If you know the hash of a sensitive blob:Remove files with specific patterns in name
Removing a Directory
To remove an entire directory from history:Complete Cleanup Workflow
1
Create a fresh clone
2
Remove sensitive data
3
Verify the cleanup
4
Replace the remote repository
5
Rotate credentials
Change all exposed passwords, API keys, and credentials immediately.
6
Notify team members
Instruct all contributors to delete their local clones and re-clone:
Post-Cleanup Checklist
- All sensitive credentials have been rotated
- Force push completed successfully
- All team members notified to re-clone
- GitHub/GitLab support contacted (if needed for server-side cleanup)
- Repository size reduced as expected
- CI/CD secrets updated
- Documentation updated with new credentials (in secure storage)
