If the repository already exists, you have to do the following:
git rm -r --cached .
git add .
git commit -m ".gitignore is now working"