The reason for the invalidation is that the corresponding directory or file has been tracked by git, and it will be invalid after adding .gitignore at this time.
Folder: git rm -r --cached .idea
File: git rm --cached project.iml
Re-add the .gitignore file