How to Find and Remove Hardcoded Secrets in Your Code
Hardcoded secrets — API keys, database passwords, tokens — are one of the most common causes of data breaches. They get committed by accident and sit in your Git history for years. Here is how to find and remove them.
Step 1: Scan your repositories
Run secret detection across your code with GitGuardian. It flags exposed credentials in commits, branches, and pull requests in real time.
Step 2: Remove and rotate
Never just delete a key — it may already be leaked. Rotate the credential in the provider console first, then remove it from the code and push a clean commit.
Step 3: Add a code-quality gate
Layer in SonarQube to catch security issues and code smells in your pipeline, so problems are blocked before they merge.
Step 4: Prevent future leaks
Move secrets to a secrets manager or environment variables, enable pre-commit hooks, and review your .gitignore so config files never get committed.
Treat every detected secret as compromised until you have rotated it. Detection is step one; rotation is what actually closes the risk.
