Lesson 4 of 5 · 2 min
Keeping secrets out
Keys and passwords do not belong in history. List private files in .gitignore and check what you are about to commit.
printf '.env\n*.key\n' >> .gitignore git diff --staged
If a key has been pushed, treat it as known to everyone: revoke it and make a new one. Deleting the file afterwards does not remove it from history.
import re
staged = 'API_URL="https://example.com"\nAPI_KEY="sk-EXAMPLE1234567890"\n'
for number, line in enumerate(staged.splitlines(), 1):
if re.search(r"(key|token|secret|password)\w*\s*=\s*\S{8,}", line, re.I):
print("check line", number, "before committing")
