I have this two liner as part of my post-os-install setup script:
curl -fsSL https://www.toptal.com/developers/gitignore/api/linux > ~/.gitignore
git config --global core.excludesFile ~/.gitignore