Ignore Files (.gitignore)

Why some files shouldn't be tracked

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:

  • Secrets — a .env file often holds passwords and API keys. Push it and your secrets are on GitHub.
  • Dependenciesnode_modules can be tens of thousands of files, all reinstallable with one npm install.
  • Build output — folders like dist/ are generated from your code; the code is the thing worth saving.
  • OS junk — files like .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.

Create a .gitignore

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

Terminal
$ node_modules/
$ .env
$ dist/
$ *.log
In VS Code
  1. In the Explorer panel, click the New File icon and name it .gitignore
  2. Add one pattern per line, as shown
  3. Save — Git immediately stops seeing those paths

The 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.

The pattern syntax, decoded

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

Terminal
$ git check-ignore -v dist/main.js
$ # prints the .gitignore line that matches — silence means it's NOT ignored
In VS Code
  1. Quick visual check: ignored files turn grey in the Explorer the moment you save .gitignore
  2. For the exact rule doing the ignoring, use the integrated terminal (Terminal → New Terminal)

Tip: 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.

"It's already tracked!" — .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

Terminal
$ git rm --cached secrets.env
$ git rm -r --cached node_modules   # a whole folder needs -r
$ git commit -m "Stop tracking ignored files"
In VS Code
  1. Easiest in the terminal — the Source Control panel doesn't expose 'untrack' directly (use Terminal → New Terminal)

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

Frequently Asked Questions

What should go in a .gitignore file?
Secrets (.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.
I committed a file by mistake — how do I stop tracking it?
Add it to .gitignore, then run git rm --cached <file> and commit. That removes it from Git's tracking without deleting your local copy.
Does .gitignore delete the files from my disk?
No. .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.
Can I ignore a file only on my machine?
Yes — add the pattern to .git/info/exclude inside your repo. It uses the same syntax as .gitignore but is never committed, so teammates never see it.