What is a .gitignore file, and what belongs in it?
Quick Answer
.gitignore lists file patterns Git should not track — build output (bin/, obj/), dependencies (node_modules/), IDE files (.vs/, .idea/), logs, and secrets / local config (appsettings.Development.json if it holds credentials, .env). If a file is already tracked, adding it to .gitignore does nothing — use git rm --cached.
.gitignore tells Git which files to leave alone — compiled output like bin and obj, node_modules, IDE folders, log files, and anything with secrets like a local .env. The gotcha people hit is that if a file is already committed, adding it to .gitignore won't untrack it — you have to git rm --cached it. And OS-level junk like .DS_Store goes in a global gitignore on my machine rather than every project's file.
- Start from the language template (GitHub maintains a good
VisualStudio.gitignore). - A global gitignore (
~/.gitignore_global) is for machine/OS files (.DS_Store,Thumbs.db) that shouldn't clutter every repo's file. - Never commit secrets — if you already did, rotate them; removing them from history is hard and the old commit is still out there.