We have existing git repository which contains e.g. .net projects written in C# and we want to apply gitignore rules.
At the beginning we need to crate our own ignore rules or download predefined from https://github.com/github/gitignore.
In this case I prefer download predefined file: VisualStudio.gitignore and put in my existing git repository.
Don't forget commit all pending changes. After that run below commands
git rm -r --cached .
Below command removes everything from the index
git add .
git commit -m ".gitignore is now working"