A Node project on a Mac in VS Code: what each line of its .gitignore does
Tick Node, macOS and VS Code and this page writes three sections. The Node section starts with node_modules/, the folder npm fills with every dependency. It can hold hundreds of megabytes and is rebuilt from package.json and the lock file, so it never belongs in the repository. The trailing slash matters: it tells git to match only directories, so a stray file called node_modules would still be seen. The same goes for dist/, build/ and coverage/, which are produced by your tools and recreated on every build.
Next come .env, .env.* and !.env.example. The first two keep local settings files out, which is where API keys and database passwords usually live. The third line starts with !, which means "not ignored after all". Because .env.* matched .env.example, the ! line puts it back, so you can commit a template that shows which settings exist without their values. We checked this in a fresh repository: .env and .env.local were hidden from git status, and .env.example was listed as a new file.
The macOS section is short: .DS_Store, the hidden file Finder leaves in every folder it opens to remember icon positions, plus ._* and a few others the system creates on shared drives. None of them are useful to anyone else. The VS Code section shows a subtlety worth knowing. It ignores .vscode/* and then puts back .vscode/settings.json, tasks.json, launch.json and extensions.json, the files a team may want to share.
Why .vscode/* and not .vscode/? Because git cannot re-include a file whose parent folder is ignored. When a folder is ignored, git does not even look inside it, so the ! lines never get a chance to match. We tested this too. With .vscode/ followed by !.vscode/settings.json, git status showed nothing from the folder at all. With .vscode/* instead, settings.json appeared and the other files stayed hidden. The star ignores the folder’s contents, not the folder, and that leaves room for exceptions.
The page joins the sections in the order you ticked them and writes each pattern once. If you add Next.js to this project, its node_modules/, .env and *.tsbuildinfo lines are skipped because Node already wrote them, and only the new ones, such as .next/ and .vercel/, appear under its heading. Other templates use glob features you will see in the wild: Python’s *.py[cod] matches .pyc, .pyo and .pyd files with one line, and ** matches any number of folders.
One rule catches nearly everyone. .gitignore decides what git will start tracking, not what it already tracks. If node_modules or a .env file was committed before the ignore line existed, it stays in the repository and keeps showing changes. Run git rm -r --cached node_modules (or the file name), commit, and the ignore rule takes over from there. If a secret was committed, also change the secret: it is still in the history.