How to Make Git Forget About a Tracked File Which is Now in .gitignore

You may encounter situations when there is a file that was being tracked by Git, but now it is on the .gitignore list. However, if you run git status it keeps showing the file after it is edited.

Watch a course Git & GitHub - The Practical Guide

Steps to removing the file from Git

Now let’s see what steps you need to make Git completely forget about that file that was tracked from .gitignore:

  1. Removing the file from the index

    As Git will continue to track any files that are already being tracked, you should remove the file from index to stop tracking it:

    git rm --cached <file>
  2. Tracking files

    Track the files that should be tracked:

    git add .
  3. Preventing the file be committed to repository

    If the file that is ignored would be modified, after modifying and executing the second step, it would be added to the index. Next commit would commit it to the repository. For avoiding this execute the following:

    git update-index --assume-unchanged <path&filename>

Tracked, Untracked and Ignored Files

Git accepts three kinds of files: tracked, untracked, and ignored. Ignored files are the ones that are ignored by Git. Tracked files are the files that were in the last snapshot. They can be unmodified, modified, or staged. Untracked files are the files in your working directory that were not in your last snapshot and are not in your index.