Learn how to keep your sensitive .env files out of GitHub with step-by-step tips on using .gitignore, removing tracked files, and cleaning history safely.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Understand Why You Should Keep .env Files Out of Git
When you build applications, you often store secrets like API keys, database credentials, or tokens in a .env
file. Uploading this file to GitHub can expose those secrets to the public or to anyone with access to your repository.
Step 2: Create or Update Your .gitignore File
A .gitignore
file tells Git which files or directories to ignore when you stage changes.
If you don’t already have a .gitignore
file in your project root, create one. Then add an entry for .env
to ensure Git will skip it:
# Ignore environment variable files
.env
Step 3: Remove Already Tracked .env Files from Git
If you’ve committed .env
in the past, simply adding it to .gitignore
won’t untrack it. You must remove it from the index while keeping it on disk:
# Stop tracking .env but keep it locally
git rm --cached .env
# Commit the change
git commit -m "Remove .env from tracking and add to .gitignore"
Step 4: Create a .env.example Template
To help collaborators understand which environment variables are needed without exposing real values, create a .env.example
file. Include only the keys and placeholder values:
# Copy your real .env to .env.example and scrub secrets:
cp .env .env.example
# Then open .env.example and replace values with placeholders:
# DB_HOST=your_database\_host
# DB_USER=your_username
# DB_PASS=your_password
.env.example
so teammates know what variables to define..env.example
to .gitignore
.
Step 5: Verify .env is Being Ignored
Make sure Git is no longer tracking your .env
file by running:
# Attempt to stage .env again
git add .env
# Check status to confirm .env is not staged
git status
.env
in the staged or unstaged list..gitignore
syntax and file location.
Step 6: Adopt Best Practices for Managing Secrets
Step 7: Clean Up History if You’ve Leaked Secrets
Simply removing a file in a new commit doesn’t remove it from past commits. If you have accidentally pushed a .env
file with real secrets, you should rewrite history and force-push:
# Install the BFG Repo Cleaner or use git filter-branch
# Example with BFG to remove all .env files:
bfg --delete-files .env
# After cleaning, force-push:
git push --force
Step 8: Document Your Workflow
In your project’s README, add a section describing how to set up environment variables safely:
## Environment Setup
1. Copy .env.example
into .env
2. Fill in your API keys and database credentials
3. Do not commit .env
—it is already added to .gitignore
Following these steps ensures your secrets remain private, your Git history stays clean, and collaborators know exactly how to configure their environments without risking exposure.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.