.gitignore and what never goes in a repo

Reading · 5 min · Module 5, lesson 5 of 610 min left in this module

Module 5 · Git essentialsLesson 5 of 6

Goal: Write a .gitignore for an app before its first commit, and know that ignoring a file doesn't remove one already committed.

Key idea

A .gitignore file lists what Git should never add: files you can rebuild, files that belong to one machine, and secrets. Write it before the first commit. It only stops new files from being added; anything already committed stays in the history.

What stays out

A repository holds what someone needs to rebuild the app, and nothing that one machine made or that nobody else should see.

  • Dependencies: node_modules/. They're large, and npm install rebuilds them from package.json.
  • Build output: dist/, build/, .next/. The build makes these again.
  • Local configuration and secrets: .env, which holds your real values (lesson 2.4.2). Commit .env.example with placeholders instead.
  • Local databases: files like the app.db from lesson 2.3.5. They hold your test data, and the migrations rebuild the schema.
  • Logs and OS files: *.log, .DS_Store (macOS), Thumbs.db (Windows).

A .gitignore for a Node app

Put a file called .gitignore in the repository's top folder, and commit it:

# Dependencies
node_modules/

# Build output
dist/
build/

# Local configuration and secrets
.env

# Local databases
*.db

# Logs and OS files
*.log
.DS_Store
Thumbs.db

A trailing / matches a folder, * matches any characters, and # starts a comment. Run git status afterwards: the ignored files should no longer be listed.

Before the first commit of any project, run git status and read the list. Every file on it is about to become part of the history. If you see a folder of dependencies, a database file or a .env, add a rule first.

Starting a project in another language? Search for a .gitignore template for it (GitHub keeps a collection) and trim it to what you use.

Ignored isn't removed

.gitignore only affects files Git isn't tracking yet. If .env was committed last week, adding it to .gitignore today changes nothing: Git keeps tracking it and shows every edit.

To stop tracking it without deleting your copy:

git rm --cached .env
git commit -m "Stop tracking .env"

That removes it from the next commit, not from the history. Every earlier commit still contains it, and anyone with a copy of the repository can read it. If it held a secret, treat the secret as leaked: rotate it first, then clean the history, as you'll do in lesson 2.6.3.

Check why a file is ignored, or isn't

git check-ignore -v app.db prints the .gitignore line that matches, such as .gitignore:12:*.db app.db. It prints nothing if the file isn't ignored, or if Git already tracks it, which is the clue that the file was committed before the rule existed.

git add -A respects .gitignore. git add -f forces an ignored file in, so don't use it without a reason.

Check yourself

You add .env to .gitignore, but git status still shows .env as modified. Why?
Which of these should be committed?