Add file to .gitignore that was already tracked

If you want to ignore a file within your git repository that was already tracked by the system, you have to delete the file from the repository cache. After deleting the file, you can add the ignore rule to your .gitignore file and it will be ignored by the repository.

First: Delete the file from the repository cache

git rm --cached path/file

Second: Add the ignore rule to your .gitignore file

path/filename

Third: Commit the updated .gitignore file

git add file
git commit -m "Updated .gitignore rule"
git push

Good luck.