How to Keep a .env File Out of Git with .gitignore
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
.env
git rm --cached <file>
| Part | What it means |
|---|---|
.env in .gitignore | Tells 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:
git init
git status
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 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:
- bash/zsh
- PowerShell
echo ".env" >> .gitignore
Add-Content -Path .gitignore -Value ".env"
>> adds the text at the end of the file, and so does Add-Content. The file now holds a 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:
git check-ignore -v .env
git status
.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)

.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
git add .
git commit -m "Add the app and .gitignore"
git ls-files
[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

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:
git add .
git commit -m "Add the app"
[master (root-commit) 2053625] Add the app
2 files changed, 2 insertions(+)
create mode 100644 .env
create mode 100644 app.py

create mode 100644 .env means the key is now in a commit. Adding the line to .gitignore afterwards doesn't change that:
echo ".env" >> .gitignore
git status
git ls-files
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

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."
git rm --cached .env
git status
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

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:
git add .gitignore
git commit -m "Stop tracking .env"
git ls-files
git status --ignored
[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

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:
git show HEAD~1:.env
WEATHER_API_KEY=not-a-real-key-123

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
| Mistake | What happens | Do this instead |
|---|---|---|
Adding .env to .gitignore after it was committed | git ls-files still lists .env, and Git keeps tracking it | Run git rm --cached .env and commit |
Thinking git rm --cached removed the key from the repository | git show HEAD~1:.env still printed the key from the earlier commit | Rotate 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 gone | Use >> to add a line |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
git check-ignore -v .env prints nothing | No line in any .gitignore matches the file. For app.py, which isn't ignored, it printed nothing and echo $? printed 1 | Add .env to .gitignore, then check again |
git status doesn't list .env, but git ls-files does | The file was committed before the line was added | git 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
- gitignore — the pattern format, and what ignoring can and can't do.
- git rm —
--cachedand the other options. - git check-ignore — checking which pattern ignores a path.
- Removing sensitive data from a repository — GitHub's guide, for data that can't simply be rotated.
