← All Notes
journal

Stop Tracking Files Already Committed to Git

A .gitignore rule does not affect files that Git already tracks. If an ignored file or directory was previously committed, remove it from Git’s index while keeping the local copy.

Confirm the ignore rule

Check if Git’s ignore rules match a file inside the directory:

git check-ignore -v --no-index path/to/directory/file

Output identifies the matching .gitignore file and rule.

Stop tracking the directory

From the repository root:

git rm -r --cached -- path/to/directory
  • -r processes the directory recursively.
  • --cached removes it only from Git’s index.
  • -- separates command options from the path.

The directory and its files remain on the local filesystem.

For example:

git rm -r --cached -- apps/portal/.astro

Verify the result

git ls-files -- path/to/directory

The command should return nothing.

git status will show the tracked files as staged deletions. This is expected: Git is preparing to remove them from the repository, not from the local filesystem.

Commit the removal

git commit -m "Stop tracking generated files"

Once committed, the existing .gitignore rule prevents the files from being added again through normal Git operations.