Untracking Files Git Already Knows About

By Next Feline ยท

I added .vscode to my .gitignore, committed, and was surprised to see Git still tracking changes to my editor settings. The ignore rule seemed to be doing nothing.

It turns out .gitignore only affects files Git is not already tracking. Once a file has been committed, Git keeps watching it, whatever the ignore file says.

The fix is to tell Git to stop tracking it, without deleting it from your computer:

git rm -r --cached .vscode
  • rm removes the files from Git.
  • --cached is the important part. It removes them from the repository only, leaving your local copies exactly where they are.
  • -r is needed because .vscode is a folder.

Then commit the change:

git commit -m "Stop tracking .vscode"

From now on, with .vscode in .gitignore, Git will leave it alone.

One thing to be aware of: anyone else who pulls this commit will have those files deleted from their copy, because, from Git's point of view, they were removed. For personal editor settings, that is usually fine, but it is worth knowing before doing it to anything people rely on.

It is also worth remembering that the files still exist in the repository's history. If you have accidentally committed something sensitive, such as a .env file with passwords, untracking it is not enough. The secrets are still in the old commits, so change them straight away.

For editor settings and build output, though, these two commands are all you need.