Stop tracking files ignored by .gitignore
Changing .gitignore does not affect files Git already tracks.
Ignore rules prevent matching untracked files from being added. Files already present in Git’s index remain tracked until they are removed from it explicitly.
The relevant command is:
git rm --cachedThe --cached flag removes files from the index while keeping the local copies on disk.
Example
Assume .gitignore contains:
generated/Git will ignore new files inside generated/. However, files that were added before this rule was introduced remain tracked, for example:
generated/example-file.extgenerated/cache.jsongenerated/debug.loggenerated/output/data.jsonTheir changes can therefore continue to appear in git status.
List tracked files that are now ignored
Use the following command to list files that are present in the index but match the current ignore rules:
git ls-files --cached --ignored --exclude-standardLimit the result to generated/:
git ls-files --cached --ignored --exclude-standard -- generated/Dry-run the cleanup
Before changing the index, inspect what Git would remove:
git ls-files --cached --ignored --exclude-standard -z -- generated/ \ | xargs -0 -r git rm --cached --dry-run --ignore-unmatchThe null-delimited output safely handles filenames containing spaces or other unusual characters.
Stop tracking the files
When the dry-run output is correct:
git ls-files --cached --ignored --exclude-standard -z -- generated/ \ | xargs -0 -r git rm --cached --ignore-unmatchThis stages the files for removal from the repository without deleting them from the working tree.
For a single file:
git rm --cached -- generated/example-file.extDo not omit --cached unless the file should also be deleted locally:
git rm -- generated/example-file.extReview the staged changes with git status --short, then commit the updated .gitignore and the removals from the index:
git add -- .gitignoregit commit --message "chore: stop tracking ignored generated files"List ignored untracked files
After the cleanup, the files are ordinary ignored files in the working tree.
Show ignored files alongside the normal status output:
git status --ignored --shortList only ignored untracked files:
git ls-files --others --ignored --exclude-standardLimit the result to generated/:
git ls-files --others --ignored --exclude-standard -- generated/Optionally delete ignored files locally
Use git clean only when the ignored files should also be removed from disk.
Always start with a dry-run:
git clean --dry-run -d -X -- generated/| Flag | Meaning |
|---|---|
--dry-run | Show what would be deleted |
-d | Include directories |
-X | Delete only ignored files |
Run the deletion only after reviewing the output:
git clean --force -d -X -- generated/Uppercase -X removes only ignored files. Lowercase -x also removes non-ignored untracked files and is considerably more destructive.
For generated files, caches, logs, and other local files that should remain available, git rm --cached is the appropriate operation.