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.

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.
- Create a file named
.envin your project root and add a dummy variable:MY_SECRET=12345. - Update your
.gitignorefile to include.env. - Verify your configuration by running
git status. Ensure.envdoes not appear in the "Untracked files" list. - (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
.gitignoreafterwards will not remove it from your history. You will need to use tools like BFG Repo-Cleaner orgit filter-branchto purge the history. Always verify your.gitignorebefore your first commit. - Assuming .env is enough:
.envfiles 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.
Work with me

AI Automation & Agentic Workflow Development
Automate the repetitive work eating your time — content pipelines, data workflows, and agentic AI tasks that run themselves.

React & Next.js Dashboard / Admin UI Development
A clean, data-rich dashboard UI in React or Next.js — charts, tables, and real-time data that your users will actually enjoy using.


