.gitignore Not Ignoring a File? Check Whether Git Tracks It
Adding a pattern to .gitignore only affects untracked paths. If an existing file still shows as modified after you add a rule, check whether it was already added to Git before changing the pattern.
Worked example
Diagnose a synthetic build report without staging changes
git ls-files -- build/local-report.txt git check-ignore -v --no-index -- build/local-report.txt git rm --cached --dry-run -- build/local-report.txt
Steps
- From the repository root, run git ls-files -- with the path. A printed path means Git already tracks it. Then use git check-ignore -v --no-index to see which rule would apply if the path were untracked.
- If the file should become local-only and the intended rule matches, inspect git rm --cached --dry-run -- first. When the result is expected, use git rm --cached -- with that one path to stage removal from the index; leave the local file on disk.
- Review the staged change and confirm the local file still exists. If the path is untracked and no rule matched, fix the rule or its location instead.
What to check
- Run the last command in the example only for a tracked path. The dry run does not modify the index; the real command stages a deletion from the repository in the next commit.
- Ignoring a file does not remove it from earlier commits. If it contained a credential, rotate that credential and handle repository history separately.