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.

Note
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

1
git rm --cached .

or a specific directory

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

or a specific file

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

Add the changes to the staging area and commit.

1
git add -A
1
git commit

Your .gitignore should now be respected.