.env), dependencies (node_modules/), build output (dist/), and editor/OS junk like .DS_Store. The github.com/github/gitignore repo has ready-made templates for every language.Guide
Some files should never be in version control. Here's how to tell Git to look away.
A repository should hold the files you wrote — your source code. Plenty of other files live in a project folder, and committing them ranges from wasteful to dangerous:
.env file often holds passwords and API keys. Push it and your secrets are on GitHub.node_modules can be tens of thousands of files, all reinstallable with one npm install.dist/ are generated from your code; the code is the thing worth saving..DS_Store (macOS) or Thumbs.db (Windows) that your computer sprinkles around.
The fix is a plain text file called .gitignore — a list of patterns Git should
pretend it can't see. Anything it matches never shows up in git status and never
gets committed.
Warning: set up .gitignore before your first commit, so nothing slips in by accident. It only stops files that aren't tracked yet — more on that trap below.
Make a file named exactly .gitignore (the leading dot is part of the name, and
there's no extension) in the root of your repo, with one pattern per line:
Example .gitignore
$ node_modules/
$ .env
$ dist/
$ *.log .gitignoreThe lines above are the file's contents (not commands to run). They tell Git to ignore dependencies, secrets, build output and log files.
Creating the repo on github.com instead? The new-repository page has an Add .gitignore dropdown that generates a ready-made one for your language before your first clone.
Each line is a pattern. A handful of rules cover almost everything you'll ever write:
node_modules/ — a trailing slash ignores a whole folder and everything inside it.*.log — * is a wildcard ("match anything"), so this ignores every file ending in .log./secret.txt — a leading slash anchors the match to the repo root only, not a secret.txt buried in a subfolder.!keep.log — a leading ! is an exception: un-ignore something an earlier line matched. Handy right after *.log.# note to self — lines starting with # are comments; blank lines are fine too.Not sure whether a pattern is working? Ask Git which rule (if any) is hiding a file:
Check why a file is (or isn't) ignored
$ git check-ignore -v dist/main.js
$ # prints the .gitignore line that matches — silence means it's NOT ignored .gitignoreTip: you rarely write a .gitignore from scratch. GitHub keeps ready-made templates for every language and framework at github.com/github/gitignore — copy the one for your stack and tweak from there.
.gitignore doesn't untrack
The classic surprise: you add node_modules/ to .gitignore, yet Git
keeps listing its changes. That's because .gitignore only stops Git from picking
up new files — anything already committed stays tracked. To stop tracking a file
while keeping your local copy, use git rm --cached (--cached means
"remove it from Git's tracking list only, not from my disk"):
Untrack a file committed by mistake
$ git rm --cached secrets.env
$ git rm -r --cached node_modules # a whole folder needs -r
$ git commit -m "Stop tracking ignored files" Make sure the pattern is in .gitignore first, or the file just reappears as untracked. And if a secret was ever pushed, also rotate it (change the password/key) — Git history keeps the old versions.
Up next: now that your repo only tracks what matters, learn to read its story — Viewing History covers log, show, diff and blame.
FAQ
.env), dependencies (node_modules/), build output (dist/), and editor/OS junk like .DS_Store. The github.com/github/gitignore repo has ready-made templates for every language..gitignore, then run git rm --cached <file> and commit. That removes it from Git's tracking without deleting your local copy..gitignore only tells Git to stop watching those files — they stay on your disk and keep working normally. Even git rm --cached removes a file from tracking, not from your folder..git/info/exclude inside your repo. It uses the same syntax as .gitignore but is never committed, so teammates never see it.