Stop tracking a file in Git without deleting it from your disk. Learn how git rm --cached works and why adding a file to .gitignore does not untrack a file Git already tracks.
The short answer
#Use git rm --cached. It removes the file from Git's index, so the next commit stops tracking it, but leaves your copy on disk. Add it to .gitignore in the same commit so it does not get added again.
_10git rm --cached .env_10echo ".env" >> .gitignore_10git add .gitignore_10git commit -m "Stop tracking .env"
Why it happens
#Git tracks a file once it has been added to the index and committed. From then on Git keeps watching it, whatever your ignore rules say.
.gitignore only affects untracked files. It tells git add and git status which new files to leave alone. It does not untrack files that are already in the repository. That is why adding node_modules/ or .env to .gitignore after committing them seems to do nothing: Git still shows every change to them.
There are two different "remove" commands:
git rm fileremoves the file from the index and deletes it from your working folder.git rm --cached fileremoves it from the index only. Your file stays where it is and becomes untracked.
How to fix it
#Untrack a single file
_10git rm --cached config/local.json_10echo "config/local.json" >> .gitignore_10git add .gitignore_10git commit -m "Stop tracking local config"
After the commit, git status no longer reports changes to the file, and it will not be pushed.
Untrack a whole folder
Add -r to recurse into directories.
_10git rm -r --cached node_modules_10echo "node_modules/" >> .gitignore_10git add .gitignore_10git commit -m "Stop tracking node_modules"
Reapply .gitignore to everything
If you have just written or fixed a .gitignore and want every matching file untracked in one go, clear the index and add everything back. Files that match your ignore rules will not be re-added.
_10git rm -r --cached ._10git add ._10git status # check the list of deleted files before committing_10git commit -m "Apply .gitignore"
Review git status carefully. Anything listed as deleted will stop being tracked.
Check why a file is or is not ignored
git check-ignore shows which rule matches a path. It returns nothing if no rule matches.
_10git check-ignore -v .env_10# .gitignore:3:.env .env
Ignore a file only on your machine
For files that only you need to ignore, such as editor settings, add them to .git/info/exclude instead of .gitignore. It uses the same pattern format but is never committed. For rules you want in every repository, set a global ignore file with core.excludesFile.
Common pitfalls
#- Expecting the file to stay on everyone's machine. Your commit records the file as deleted, so when teammates pull it, Git deletes their copy too. Warn them, or ask them to back the file up first.
- Thinking a committed secret is now safe.
git rm --cachedonly affects future commits; the file is still in every earlier commit and in any clone or fork. Rotate the secret straight away. Removing it from history needs a history rewrite, for example with the separategit filter-repotool, plus a force push. - Using
git rmwithout--cached. That deletes your local file as well. If you did it by mistake before committing,git restore --stagedand thengit restoreon the file bring it back. - Reaching for
git update-index --assume-unchangedor--skip-worktreeto ignore a tracked file. The Git documentation says these are not designed for ignoring changes to tracked files, and they can lead to surprising results. - Adding a folder pattern without the trailing slash.
buildmatches files and folders named build anywhere;build/matches folders only.
Related reading
#Was this resource helpful?