Skip to main content

How to Keep a .env File Out of Git with .gitignore

· 12 min read
Jagdish Kumawat
Founder @ Dewiride

A .env file holds settings such as API keys and passwords, which is exactly what must never end up in a Git repository. One line in .gitignore keeps Git from ever picking it up. This guide adds that line on Git 2.56.1, checks that it works, and then shows the harder case: a .env that was already committed, which Git stops tracking with git rm --cached but still remembers in its history.

Syntax​

.gitignore
.env
Syntax
git rm --cached <file>
PartWhat it means
.env in .gitignoreTells Git to ignore every file called .env: Git won't list it as a new file, and git add . skips it
git rm --cached <file>Stops tracking a file Git already tracks, and leaves the file itself on your disk

A line with no slash, such as .env, matches in every folder of the repository. Git's gitignore documentation explains that a pattern with a slash at the beginning or in the middle is tied to the folder of the .gitignore file, and "Otherwise the pattern may also match at any level below the .gitignore level." For --cached, the git rm documentation says: "Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone."

The Example​

A small project folder, weather-app, holds a one-line script, app.py, and a .env file with a made-up key: WEATHER_API_KEY=not-a-real-key-123. The first part keeps that .env out of a new repository. The second part uses a copy of the folder, leaked-app, where the .env was committed before anyone wrote a .gitignore.

Prerequisites​

  • Git, with your name and email set, because every commit records them. This guide uses Git 2.56.1.

Step 1: See That Git Would Pick Up .env​

Open a terminal in the weather-app folder, make it a Git repository and ask Git what it sees:

Terminal
git init
git status
Output
hint: Using 'master' as the name for the initial branch. This default branch name
hint: will change to "main" in Git 3.0. To configure the initial branch name
hint: to use in all of your new repositories, which will suppress this warning,
hint: call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
hint:
hint: Disable this message with "git config set advice.defaultBranchName false"
Initialized empty Git repository in /Users/you/source/weather-app/.git/
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
.env
app.py

nothing added to commit but untracked files present (use "git add" to track)

The terminal after git status in weather-app: no commits yet, and two untracked files, .env and app.py.

The hint: lines only explain how Git names the first branch, and you can ignore them here. What matters is the list at the end: .env is an untracked file, right next to app.py. Git doesn't know it holds a secret. A git add . now would stage it, and the next commit would store the key in the repository.

Step 2: Add .env to .gitignore​

.gitignore is a plain text file in the project folder that lists what Git should ignore, one pattern per line. This command adds the line .env to it, and creates the file if it doesn't exist yet:

Terminal
echo ".env" >> .gitignore

>> adds the text at the end of the file, and so does Add-Content. The file now holds a single line:

.gitignore
.env

The .gitignore file in a macOS-style code window, with the single line .env.

If your project already has a .gitignore, as projects made by tools such as uv init do, the same command adds .env below the lines that are there.

Step 3: Check That Git Ignores It​

git check-ignore -v tells you whether Git ignores a path, and which line of which file makes it:

Terminal
git check-ignore -v .env
git status
Output
.gitignore:1:.env .env
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
app.py

nothing added to commit but untracked files present (use "git add" to track)

The terminal after the echo command, git check-ignore -v .env, which prints .gitignore:1:.env and .env, and git status, which now lists only .gitignore and app.py.

.gitignore:1:.env reads as "file .gitignore, line 1, pattern .env", followed by the path it matched. git status no longer mentions .env. The new .gitignore shows up instead, because it's a file that belongs in the repository.

Step 4: Commit Without the Key​

Terminal
git add .
git commit -m "Add the app and .gitignore"
git ls-files
Output
[master (root-commit) 8f7294c] Add the app and .gitignore
2 files changed, 2 insertions(+)
create mode 100644 .gitignore
create mode 100644 app.py
.gitignore
app.py

The terminal after git add, git commit and git ls-files: the commit created .gitignore and app.py, and git ls-files lists only those two files.

git add . added everything except what .gitignore excludes, and the commit created exactly two files. git ls-files lists the files Git tracks: .gitignore and app.py, and no .env. Your commit ID will differ from 8f7294c. The .env file itself is still on your disk, so your program keeps reading it.

If .env Is Already in Git​

Ignoring works only for files Git doesn't track yet. The leaked-app copy shows what happens when the .env was committed first:

Terminal
git add .
git commit -m "Add the app"
Output
[master (root-commit) 2053625] Add the app
2 files changed, 2 insertions(+)
create mode 100644 .env
create mode 100644 app.py

The terminal after git add and git commit in leaked-app: the commit created .env and app.py.

create mode 100644 .env means the key is now in a commit. Adding the line to .gitignore afterwards doesn't change that:

Terminal
echo ".env" >> .gitignore
git status
git ls-files
Output
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore

nothing added to commit but untracked files present (use "git add" to track)
.env
app.py

The terminal after adding .env to .gitignore in leaked-app: git status lists only the new .gitignore, and git ls-files still lists .env and app.py.

git status looks clean, but git ls-files still lists .env: Git keeps tracking it, and would commit every future change to it. The gitignore documentation says so in one line: "Files already tracked by Git are not affected". Its notes give the way out: "To stop tracking a file that is currently tracked, use git rm --cached to remove the file from the index."

Terminal
git rm --cached .env
git status
Output
rm '.env'
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: .env

Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore

The terminal after git rm --cached .env: it prints rm '.env', and git status shows deleted .env as a change to be committed, with .gitignore untracked.

deleted: .env under "Changes to be committed" means the next commit removes the file from the repository, not from your disk. Commit that, together with the .gitignore:

Terminal
git add .gitignore
git commit -m "Stop tracking .env"
git ls-files
git status --ignored
Output
[master d56dd6b] Stop tracking .env
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 .env
create mode 100644 .gitignore
.gitignore
app.py
On branch master
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
.env

nothing to commit, working tree clean

The terminal after committing: delete mode .env and create mode .gitignore, git ls-files lists .gitignore and app.py, and git status --ignored lists .env under Ignored files.

Git no longer tracks .env, and git status --ignored shows the file is still on disk, now ignored. But the first commit hasn't changed:

Terminal
git show HEAD~1:.env
Output
WEATHER_API_KEY=not-a-real-key-123

The terminal after git show HEAD~1:.env: it prints WEATHER_API_KEY=not-a-real-key-123 from the earlier commit.

HEAD~1 is the commit before the latest one, and git show HEAD~1:.env prints .env as it was stored there. Anyone with a copy of the repository can do the same. So once a real key has been committed, and above all once it has been pushed, treat it as leaked and replace it at the service that issued it. GitHub's guide to removing sensitive data starts there too: "If the sensitive data is a secret, such as a password, token, or credential, first revoke or rotate the secret."

Common Mistakes​

MistakeWhat happensDo this instead
Adding .env to .gitignore after it was committedgit ls-files still lists .env, and Git keeps tracking itRun git rm --cached .env and commit
Thinking git rm --cached removed the key from the repositorygit show HEAD~1:.env still printed the key from the earlier commitRotate the key at the service that issued it
Writing > instead of >>echo "*.log" > copy-of-gitignore replaced the whole file: cat printed only *.log, and the .env line was goneUse >> to add a line

Troubleshooting​

SymptomLikely causeFix
git check-ignore -v .env prints nothingNo line in any .gitignore matches the file. For app.py, which isn't ignored, it printed nothing and echo $? printed 1Add .env to .gitignore, then check again
git status doesn't list .env, but git ls-files doesThe file was committed before the line was addedgit rm --cached .env, then commit

FAQ​

Does .env also ignore .env files in subfolders?​

Yes, because the line has no slash. With config/.env in the project, git check-ignore -v config/.env printed .gitignore:1:.env and config/.env. To ignore only the file next to .gitignore, write /.env: with that line, git check-ignore -v config/.env printed nothing, and git status listed config/.env as a new file.

Does it ignore .env.example too?​

No. The pattern matches the name .env exactly: git check-ignore -v .env.example printed nothing, and git status --short listed ?? .env.example. That's useful, because many projects commit a .env.example with the names of the settings and placeholder values, so others know what to put in their own .env.

How do I replace a leaked key?​

At the service that issued it: create a new key, put it in your .env, then delete or disable the old one. For a Claude API key, How to Get a Claude API Key shows those steps in the Claude Console.

Conclusion​

The line .env in .gitignore kept the key out of the first commit, and git check-ignore -v .env showed which line did it. In a repository where .env was already committed, git rm --cached .env stopped Git from tracking it while the file stayed on disk, but git show HEAD~1:.env still printed the key from the earlier commit, so a committed key has to be rotated.

Additional Resources​