You need to put .gitignore in the working directory. It won't work if you put it in the .git (repository) directory. In other words, you can place the .gitignore file anywhere in the working directory, i.e in any folder where your code prevails. But the best practice would be to place the .gitignore file in the root directory. This means that one .gitignore file for one entire repo. This makes managing the ignored files more effectively.
$ ls -1d .git*