In this lesson, you will learn how to use .gitignore and template files to ensure that local sensitive configurations never accidentally reach your public version control history.
When developing applications, you often store database credentials or API keys in local files like .env or config.json. Because these files contain secrets, they must never be pushed to a repository like GitHub. Even if you delete a secret in a later commit, it remains permanently stored in your repository’s history, requiring complex history rewriting to remove.
The .gitignore Shield
The primary tool to prevent these leaks is the .gitignore file. By adding the filenames of your secret configuration files to this file, Git will ignore them entirely.
# .gitignore
.env
config.local.json
__pycache__/
Once listed here, Git will neither track changes to these files nor prompt you to add them to your staging area.
Using Template Files
While you want to hide your actual secrets, other developers (or your future self) need to know which configuration keys are required for the application to function. To solve this, create a "template" version of your configuration file—for example, .env.example or config.example.json.
This template file should contain the keys but empty or dummy values:
# .env.example
DATABASE_URL=
API_KEY=your_key_here
DEBUG_MODE=True
Commit this template file to your repository. When a collaborator clones your project, they can simply copy the template to create their own local, untracked version:
cp .env.example .env
By keeping the template in the repo and the actual configuration in an ignored file, you provide a roadmap for setup without exposing sensitive data. This workflow ensures that your environment-specific secrets remain strictly local, while the structure of your application remains shared and reproducible.
Key Takeaway: Always commit a template file defining the required configuration keys, and use a .gitignore file to ensure the actual secret-filled configuration files are never tracked by version control.