You notice logs/ and .env in git status, add them to .gitignore, and they're still there, still modified, still waiting to be committed. The .gitignore is right. It just doesn't apply to these files.
Everything below was run on a BrokenCluster lab machine with Git, in a small repository where .env and logs/app.log were committed by mistake in the first commit.
Why .gitignore is ignored
Git has three kinds of files: tracked (in the index, so part of the next commit), untracked, and ignored. Ignore rules only apply to untracked files. They stop git status from listing new files and stop git add . from picking them up. A file that's already tracked stays tracked, whatever .gitignore says.
So after committing these files once, adding them to .gitignore changes nothing:
$ git ls-files
.env
app.py
logs/app.log
$ printf 'logs/\n.env\n' > .gitignore
$ echo "request 2" >> logs/app.log; echo "DEBUG=0" > .env
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .env
modified: logs/app.log
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
no changes added to commit (use "git add" and/or "git commit -a")
Check that your pattern is right
Before untracking anything, confirm the rule matches. git check-ignore tells you which line of which file ignores a path. For tracked files it prints nothing unless you add --no-index, which is exactly the trap this guide is about:
$ git check-ignore -v logs/app.log .env
$ git check-ignore -v --no-index logs/app.log .env
.gitignore:1:logs/ logs/app.log
.gitignore:2:.env .env
The patterns match. The files are ignored in principle and tracked in practice.
Untrack them without deleting them
git rm --cached removes a file from the index only. Your copy on disk stays where it is. Use -r for directories:
$ git rm -r --cached logs .env
rm '.env'
rm 'logs/app.log'
$ git status --short
D .env
D logs/app.log
?? .gitignore
$ git add .gitignore && git commit -m "Stop tracking logs and .env"
[main f0b4c77] Stop tracking logs and .env
3 files changed, 2 insertions(+), 2 deletions(-)
delete mode 100644 .env
create mode 100644 .gitignore
delete mode 100644 logs/app.log
Git shows D (deleted) because the commit removes the files from the repository. On your disk, they're untouched, and from now on they're ignored:
$ ls logs .env
.env
logs:
app.log
$ git status
On branch main
nothing to commit, working tree clean
$ git ls-files
.gitignore
app.py
Commit .gitignore together with the removal, so nobody can add the files back by accident.
What your teammates see when they pull
This is the part most answers leave out. Your commit deletes .env from the repository, and a pull applies that deletion. A teammate who cloned before your fix, and hasn't touched the file, loses their copy:
$ git pull
From /home/student/app
3e67708..4081aaa main -> origin/main
Updating 3e67708..4081aaa
Fast-forward
.env | 1 -
.gitignore | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 .env
create mode 100644 .gitignore
$ ls -a
.
..
.git
.gitignore
app.py
They can get the last committed version back from the commit before the pull. Right after a pull, ORIG_HEAD points there:
$ git show ORIG_HEAD:.env > .env && git status --short && cat .env
DEBUG=1
git status prints nothing for it, because the file is ignored now. If the teammate had local changes to the file, Git refuses to pull rather than delete them:
$ git pull
error: Your local changes to the following files would be overwritten by merge:
.env
Please commit your changes or stash them before you merge.
Aborting
In that case they copy the file somewhere safe, discard the change (git restore .env), pull, and copy it back. Either way, tell your team before you push an untracking commit for a file people edit locally, such as .env or an IDE settings file.
If it held a secret
git rm --cached stops future commits from including the file. Every earlier commit still has it, and so does every clone and fork. If .env contained real passwords or keys, treat them as leaked and rotate them. Rewriting history to remove the file is possible, but it needs a force push and doesn't reach copies others already have.
In short
git check-ignore -v --no-index <path>confirms the pattern.git rm --cached <file>(or-r <dir>) untracks without deleting.- Commit together with
.gitignore. - Warn teammates: their pull removes their copy.
Questions
- Why doesn't .gitignore work for a file I already committed?
- Ignore rules only decide which untracked files git status shows and git add picks up. A file that is already in the index is tracked, and Git keeps tracking its changes no matter what .gitignore says.
- How do I stop tracking a file but keep it on disk?
- Remove it from the index only: git rm --cached <file> (add -r for a directory), make sure .gitignore lists it, and commit. The file stays in your working tree.
- Will the file be deleted for my teammates?
- Yes, if they haven't changed it: when they pull your commit, Git removes the file from their working tree, because the commit deletes it from the repository. If they have local changes to it, the pull stops instead, so they can save their copy first.
- Does untracking remove the file from history?
- No. Every earlier commit still contains it. If it held a secret, rotate that secret: removing a file from history needs a history rewrite and a force push, and copies may already exist.
Fix it yourself
A live machine in your browser, already broken. The grader checks the result.
- Stop Tracking Build Output and Logseasy 5 min
- Ignore Everything Except a Few Filesmedium 7 min
- Clean Build Junk, Keep Your Local Secretseasy 6 min
- .gitignore and Tracked Fileseasy 1 min
No account needed for your first tries.
More guides
- Kubernetes Service has no endpoints: the 3 causes and how to check each
Your Service exists, but requests fail and its EndpointSlice is empty. Three causes explain almost every case: the selector, readiness, or no Pods at all.
- sudo echo >> /etc/hosts: Permission denied. Why sudo didn't help
sudo runs the command as root, but the >> redirect is done by your own shell, before sudo starts. Here's why, and the three correct ways to write the file.
- No space left on device: find what is filling the disk on Linux
Which filesystem is full, which directory is growing, which files are biggest, and the space a deleted file still holds. Five commands, with real output.