Almost every developer has been there: a rushed fix, a quick git push, and a few hours later you notice a real API key or bot token in a config file. The first reflex is to delete the file and commit again. That solves almost nothing, because git keeps every previous state and the key is still readable in the old commit. The correct order of actions matters, and the first step matters most.
Why speed matters
Public repositories are continuously scanned by bots looking for key-like strings in new commits, and there are many documented cases of leaked keys being abused very quickly, sometimes within minutes. Cloud keys are used to launch servers, SMS gateway keys to send thousands of messages, payment keys to attempt fraudulent transactions. "Nobody looks at my repo yet" is not a defence.
Step 1: revoke the key immediately
Before cleaning history or notifying colleagues, make the key useless. Assume it has already been copied. Delete or regenerate it in the provider's dashboard and store the new one safely. For a Telegram bot, use /revoke in @BotFather. For a leaked database password, change it and make sure the database is not reachable from outside. Your production site may stop working until you put the new value on the server; a few minutes of downtime is far cheaper than an open key. If the same key was reused in other projects or on a colleague's laptop, update it everywhere at once so nothing keeps calling the provider with the dead value and floods your logs with errors.
Example: a delivery bot
A family restaurant in Namangan takes orders through a Telegram bot. The developer published the code for a portfolio, but config.py still held the bot token and an SMS service key. Two days later the restaurant's customers start receiving strange advertising messages and the prepaid SMS balance is gone. The right sequence: revoke the token and rotate the SMS key, check the SMS log to see how many messages went where, and only then clean the repository.
Step 2: assess the damage
Review API request logs, billing history and login lists for the period the key was exposed: unknown IP addresses, unexpected charges, new users, changed settings. If customer personal data may be affected, notify them and check legal obligations with a lawyer. To find when the key appeared:
git log -p -S "SMS_API_KEY" --all
Step 3: clean the git history
The recommended tool is git filter-repo. Work on a fresh clone, since it rewrites history:
git clone https://github.com/misol/delivery-bot.git
cd delivery-bot
git filter-repo --invert-paths --path config.py
Or replace only the secret string:
echo 'OLD_TOKEN_VALUE==>REMOVED' > ../replacements.txt
git filter-repo --replace-text ../replacements.txt
The tool removes the origin remote as a safety measure, so add it back and force-push:
git remote add origin https://github.com/misol/delivery-bot.git
git push --force --all
git push --force --tags
Everyone on the team must re-clone, otherwise an ordinary push can bring the old history back. Cleaning is not a guarantee: clones and forks may already exist, old commits may stay reachable by direct link for a while, and caches may hold copies. That is exactly why revoking comes first.
Preventing a repeat
- Keep secrets in environment variables or a
.envfile listed in.gitignore, and commit only a.env.exampletemplate. - Enable GitHub secret scanning and push protection, and add a pre-commit secret check locally.
- Use separate, least-privilege keys per project and environment to limit the blast radius.
- Review what you are about to commit with
git diff --stagedbefore every push, especially after a late-night hotfix.
On sayt.uz hosting or a VPS you can keep secrets in a file outside the web root and pass them to your code as environment variables, a habit that pays off at the next rushed fix.