Back to Blog
Lesson 38 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 25, 20264 min read

Handling Sensitive Data: Protecting Secrets in Git & GitHub

Learn to protect sensitive API keys and credentials. Master using environment variables and .gitignore to keep your secrets out of your Git history.

securitygitignoreenvironment variablesbest practicesgitgithub
Close-up of software development tools displaying code and version control systems on a computer monitor.

Previously in this course, we covered tagging releases to mark stable versions of your project. Now that you're managing professional-grade releases, you must ensure you aren't accidentally leaking sensitive information into your public or private repositories.

Hardcoding secrets—like database passwords, API keys, or private tokens—directly into your source code is one of the most common and dangerous mistakes a developer can make. Once a secret is committed to Git, it lives in the history forever, even if you delete the line in a later commit.

The Principle of Decoupling

To maintain security, we follow a simple rule: Code is for logic; Environment Variables are for configuration.

Environment variables are key-value pairs stored in the operating system's memory or a local configuration file that never gets committed to version control. When your application needs a secret, it "asks" the environment for the value rather than reading it from a hardcoded string.

Using .env Files and .gitignore

In local development, we typically use a file named .env to store these variables. Since this file contains plain-text secrets, it must never be tracked by Git.

1. Create your secret file

Create a new file in your project root called .env:

Bash
# .env file
API_KEY=super-secret-key-12345
DB_PASSWORD=my-database-password

2. Configure .gitignore

If you've followed our guide on ignoring files, you know that adding a file to .gitignore prevents Git from tracking it. Open (or create) your .gitignore file and add the .env entry:

TEXT
# .gitignore
.env

Now, when you run git status, you won't see .env listed as an untracked file. Even if you run git add ., Git will respect this rule.

A Practical Example

Most modern frameworks have libraries to load these variables. For instance, if you are working in a Node.js project, you might master environment variables with dotenv to bridge the gap between your local file and your application code.

If you are using Python, you can learn how to read environment variables for security using the built-in os module.

Hands-on Exercise: Protect Your Credentials

Let’s secure your current project.

  1. Create a file named .env in your project root and add a dummy variable: MY_SECRET=12345.
  2. Update your .gitignore file to include .env.
  3. Verify your configuration by running git status. Ensure .env does not appear in the "Untracked files" list.
  4. (Optional) Create a file called .env.example. This file should contain the keys but not the actual values (e.g., MY_SECRET=your_key_here). Commit this file to Git so other developers know which variables they need to set up.

Common Pitfalls

  • Committing before Ignoring: If you accidentally commit a secret, adding it to .gitignore afterwards will not remove it from your history. You will need to use tools like BFG Repo-Cleaner or git filter-branch to purge the history. Always verify your .gitignore before your first commit.
  • Assuming .env is enough: .env files are for local development. In production environments (like cloud providers), you should use the platform's native secret management tools, as described in guides like using Kubernetes secrets for sensitive data.
  • Forgetting .env.example: If you keep your secrets secret, how will teammates know what to set? Always provide a template file that shows the required keys without exposing the actual data.

FAQ

Q: What if I already pushed a secret to GitHub? A: Treat that secret as compromised. Rotate the API key or change the password immediately via the service provider's dashboard, then remove the file from your repository history.

Q: Can I use environment variables for non-sensitive data? A: Yes! They are excellent for environment-specific configuration (like switching between development and production database URLs) even when no secrets are involved.

Q: Are environment variables secure in production? A: They are safer than code, but for high-security production environments, look into dedicated secret managers (like AWS Secrets Manager or HashiCorp Vault).

Recap

We’ve learned that security is a proactive process. By using .gitignore to hide our .env files and utilizing environment variables to handle configuration, we keep our credentials safe from prying eyes. Remember: never commit your secrets, and always provide an .env.example file for your team.

Up next: We will look at how to fix mistakes by recovering deleted commits using git reflog.

Similar Posts