How to Git ignore files that were added to the repository

If you update .gitignore to ignore a file that you previously committed to the repository you might expect that the file gets automatically removed but it won’t happen.

If the files are in the repositories you have to remove them manually first.

Here is how you can do it.

First commit or discard any outstanding code changes, and then, run the following commands:

Removes the files from the staging area (index):

All files in the current directory

git rm --cached .

or a specific directory

git rm -r --cached ./path/to/directory

or a specific file

git rm --cached ./path/to/file

Add the changes to the staging area and commit.

git add -A
git commit

Your .gitignore should now be respected.