It happens way too often. You just set up a brand new repository for the project, you push the initial commit, you check everything is ok and... damn, you forgot the .gitignore. Now you add the .gitignore but the useless files are already in the last commit, and they won't go away.
With this command you can untrack the newly ignored files, so they won't show up in any future commit:
git rm -r --cached .
After this you can go on with the usual
git add .