Guides › Hosting, domain & email › Security & hacks

Database Credentials in Code Repos: Move to Environment Variables

Secrets.

Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Security and hacking

Database passwords committed to a code repository stay in its history even after you delete them. Move them to environment variables and rotate them.

1Find the credentials2Change the password now3Move values out4Read them in code5Ignore the file6Clean the history7Limit the database userFixed? Test the site. If not, get help.
The steps in order. Work down the list and stop when the problem is solved.

Step 1: Find the credentials

Illustration for Step 1: Find the credentials

Search the code and history for DB_PASSWORD, password= and connection strings.

Step 2: Change the password now

Illustration for Step 2: Change the password now

Create a new database password in the hosting panel and update the app. Assume the old one is exposed.

Step 3: Move values out

Illustration for Step 3: Move values out

Store them in a .env file or the host's environment variable settings, outside the public web root.

Step 4: Read them in code

Illustration for Step 4: Read them in code

Use getenv() in PHP, process.env in Node or os.environ in Python.

Step 5: Ignore the file

Illustration for Step 5: Ignore the file

Add .env to .gitignore and commit an example file with fake values.

Step 6: Clean the history

Illustration for Step 6: Clean the history

If the repo is public or shared, remove the secret from history with git filter-repo and force push.

Step 7: Limit the database user

Illustration for Step 7: Limit the database user

Give the app user only the permissions it needs.

Still stuck?

If these steps do not solve it, we fix it for you for a flat $99. Full refund if we do not solve it within 7 days.

Related guides

Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.

Last updated 2026-11-22.