Git is a powerful tool, but sometimes mistakes happen: credentials, API keys, or sensitive files end up in commits. Even if you remove them in the latest commit, they may still live in your Git history. I ran into this issue a few days ago 😭and was frantically searching for a way to get rid the sensitive data without installing additional softwares & purely using Git. (learnt a lot that day 🤔)
This guide walks you through safely removing sensitive data from Git history, step by step, while keeping your repo intact and your workflow professional. ✨
- Exposed credentials in public repositories can be exploited by attackers.
- Simply deleting a file or changing a password in the latest commit is not enough.
- Understanding Git history cleanup is essential for professional code hygiene.
Before touching history, create a mirror backup:
git clone --mirror <your-repo-url> repo-backup.gitThis ensures you can restore your repo if something goes wrong.
Decide whether you want to remove:
- A specific file (e.g., config.py containing passwords)
- A specific string (e.g., postgres:postgres, API keys, tokens)
If the file still exists in history:
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch path/to/file" \
--prune-empty --tag-name-filter cat -- --all--prune-emptyremoves empty commits--tag-name-filter catkeeps tags intact
Even if the file was deleted or renamed:
git filter-branch --force --tree-filter '
find . -type f -exec sed -i "s/SECRET_STRING/REMOVED/g" {} +
' --prune-empty --tag-name-filter cat -- --all- Replace SECRET_STRING with your sensitive value
- Replace REMOVED with a safe placeholder or nothing
git reflog expire --expire=now --all
git gc --prune=now --aggressiveThis prunes old commits and objects from Git’s internal storage.
git push origin --force --all
git push origin --force --tagsgit stash clear
Stashes may contain sensitive data too.- Move credentials to a .env file:
POSTGRES_USER=postgres
POSTGRES_PASSWORD=new_secure_password- Add .env to .gitignore:
.env- Update your app to read credentials via environment variables.
Locally
git log -p | grep SECRET_STRINGIf nothing appears → success!
Repeat with variations if needed (e.g., different passwords or API keys).
Or, On GitHub / Remote
- Go to your repository on GitHub.
- Use the Search bar in your repo (make sure “In this repository” is selected).
- Search for your sensitive string, e.g., postgres:postgres.
- If no results appear, the secrets have been successfully removed from all pushed commits.
You can also search for other common secrets like password, api_key, etc., to be thorough.
This workflow ensures sensitive data is fully removed while keeping your Git history clean and your projects secure. No need to download additional softwares for wiping your git history.
Thanks for reading 📖🙇🏻♀️ Have a great day! ( or night 🦉💻) You can follow me on twitter 🐧.