Ignoring Files That Are Already Committed
How to stop tracking files that remain tracked after being added to .gitignore with git rm --cached, how to find tracked files matched by ignore rules, and what to do first when secrets were committed.
This is the most frequently asked question. .gitignore applies only to files that are not yet tracked. A file that has been committed once keeps showing changes even after you add a rule. To stop tracking it, you must remove the file from the index (the staging area).
A single file or a single folder
# 1) Add the rule to .gitignore (e.g. .env, dist/)
# 2) Remove from the index only — the actual files in the working folder stay
git rm --cached .env
git rm -r --cached dist/
# 3) Commit
git commit -m "Stop tracking .env and dist"
--cached is the key. Without this option, git rm also deletes the files in your working folder. Directories need -r.
Finding tracked files matched by ignore rules
To see what gets caught after applying a new template, use the following command.
git ls-files -ci --exclude-standard
-c means tracked files, -i means only those matching ignore rules, and --exclude-standard means apply .gitignore, .git/info/exclude and the global setting. If the list looks right, you can untrack them all at once.
# bash / zsh
git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached --
# PowerShell
git -c core.quotePath=false ls-files -ci --exclude-standard | ForEach-Object { git rm --cached -- "$_" }
The generator's Untrack tab can build these commands for your shell.
Re-applying everything
If you changed .gitignore substantially, a common approach is to empty the index and add everything again.
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"
git add . applies the ignore rules when re-adding, so ignored files are left out. Still, always check the list of deletions with git status before committing. In repositories where the line ending setting (core.autocrlf) has changed, many files may show up as modified.
Impact on other people
When you push an untracking commit, the files are deleted from the working folders of other people who pull that commit. From git's point of view, the file was removed from the repository. If these are files everyone needs individually, such as .env or IDE settings, tell people in advance so they can back them up.
If secrets were already pushed
Untracking affects only future commits. The file remains in past commits, and anyone can retrieve it from history. Follow this order.
- Revoke the exposed keys and passwords first and issue new ones. This is the most important step.
- Untrack the file and add it to .gitignore.
- If needed, remove the file from history with a tool such as git filter-repo and force push. Copies that were already cloned or forked cannot be erased, so this does not replace step 1.
assume-unchanged and skip-worktree are not ignore features
git update-index --assume-unchanged and --skip-worktree temporarily hide changes to tracked files, but the files remain tracked.
--assume-unchangedis a performance hint. It promises git that it does not need to check the file for changes, and git may disregard this flag when it needs to.--skip-worktreeis a flag telling git not to touch the file in the working folder, and is used for sparse checkout.
The git documentation also advises against using these options to ignore changes to tracked files. For shared configuration files, it is safer to commit config.example.json and ignore the real config.json.
References
- git-rm official documentation
- git-ls-files official documentation
- git-update-index official documentation: NOTES
- GitHub Docs: Ignoring files
- Verified as of: 2026-09-23 (commands checked with git 2.50.1)