Ignoring files

Topic 5 of 5 in Your first repository6 min readBeginnerFree course

Overview

Not everything in a project folder belongs in Git. Downloaded dependencies, compiled output, log files, local databases and — above all — secrets should stay out. A .gitignore file tells Git which files to ignore, so they never show up in git status and can’t be committed by accident with git add ..

A first .gitignore

Create a file called .gitignore at the top of the repository:

Text
# Dependencies
node_modules/

# Build output
dist/
build/

# Logs
*.log

# Environment variables and secrets
.env

Then commit it, so everyone on the team gets the same rules:

Terminal
git add .gitignore
git commit -m "Add .gitignore"

Ignored files are still on disk and your programs use them as normal. Git just behaves as if they weren’t there.

Pattern syntax

.gitignore patterns. node_modules/ matches any directory named node_modules. .log matches every file ending in .log. /build matches build at the repository root only. .env matches a file named .env anywhere. !.env.example means: but do track this one (negation). docs/**/.pdf matches PDFs at any depth under docs/. Lines starting with # are comments. Already-tracked files are not affected — untrack them with git rm –cached first.

The rules in more detail:

  • Blank lines and lines starting with # are ignored.
  • * matches anything except /; ? matches one character; [abc] matches one of those characters.
  • A pattern ending in / only matches directories.
  • A pattern containing a / at the start or in the middle is relative to the .gitignore file’s location. /build matches only the top-level build; build matches any file or folder called build anywhere.
  • ** matches any number of folders: logs/**/*.txt.
  • ! re-includes something an earlier pattern excluded: !.env.example. One catch — you can’t re-include a file if its parent folder is excluded, because Git doesn’t look inside ignored folders at all.

You can also put .gitignore files in subfolders; their patterns apply to that folder and below.

Templates

You rarely need to write one from scratch. GitHub maintains a collection of templates for every language and framework at github.com/github/gitignore, and GitHub offers to add one when you create a repository. Start from the template and add your project’s specifics.

Ignoring a file you already committed

.gitignore only affects untracked files. If a file is already tracked, adding it to .gitignore does nothing — Git keeps tracking changes to it.

To stop tracking it but keep it on your disk:

Terminal
git rm --cached debug.log          # remove from the staging area only
echo "*.log" >> .gitignore
git add .gitignore
git commit -m "Stop tracking log files"

For a folder, add -r: git rm -r --cached dist/.

Two important caveats:

  • The file stays in earlier commits. That’s fine for a log file — but if it was a secret, removing it now doesn’t make it secret again. Anyone with the history can still read it. See Keeping secrets safe.
  • When teammates pull your commit, Git deletes the file from their working directory (it was tracked, now it isn’t). Warn them if it’s something like a local config file they need.

Why is this file ignored?

When a file mysteriously won’t show up in git status, ask Git:

Terminal
git check-ignore -v .env
# .gitignore:9:.env*	.env

The output gives the file, line number and pattern responsible. If nothing is printed, the file isn’t ignored.

To list everything that’s currently ignored: git status --ignored.

Personal ignores

Some files are about you, not the project: your editor’s settings folder (.idea/, .vscode/), macOS’s .DS_Store, Windows’ Thumbs.db. Rather than adding them to every project’s .gitignore, put them in a global ignore file:

Terminal
git config --global core.excludesFile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_global

(Some teams do commit shared editor settings such as .vscode/settings.json deliberately, so they can agree on formatting. That’s a team decision.)

For ignores that apply to just one repository and shouldn’t be shared, use .git/info/exclude — same syntax, never committed.

What should always be ignored?

  • Dependencies you can reinstall: node_modules/, vendor/, .venv/.
  • Build output: dist/, build/, *.class, __pycache__/.
  • Logs and temporary files: *.log, *.tmp, coverage reports.
  • Secrets: .env, private keys, credentials files. Commit an .env.example with the variable names but no values, so others know what to set.
  • Local data: databases, uploaded files, caches.

The exception: lock files such as package-lock.json and poetry.lock should be committed — they make sure everyone installs exactly the same dependency versions.

Do it first: add .gitignore before your first git add .. It’s much easier to never commit a file than to remove it from history later.

Exercise

Write a .gitignore for a Node.js project

The script creates a small Node.js project that already contains things that should never be committed: node_modules/, a dist/ build folder, log files, a .env file with a secret and an editor folder. Write a .gitignore so git status only shows .gitignore, package.json, src/ and .env.example (keep the example file!). Then fix a past mistake: debug.log was committed earlier — stop tracking it without deleting it from disk. Finally, ask Git which rule ignores .env.

Terminal
mkdir api && cd api && git init -b main
echo "debug" > debug.log
git add debug.log && git commit -m "Oops: commit a log file"

echo '{"name": "api"}' > package.json
mkdir -p src node_modules/express dist .vscode
echo "console.log('hi')" > src/index.js
echo "module" > node_modules/express/index.js
echo "bundle" > dist/app.js
echo "error" > error.log
echo "API_KEY=sk_live_123" > .env
echo "API_KEY=" > .env.example
echo "{}" > .vscode/settings.json

# 1. Write .gitignore

# 2. Stop tracking debug.log (keep the file)

# 3. Which rule ignores .env?

Summary

  • A .gitignore file lists patterns for files Git should never offer to track: dependencies, build output, logs, secrets.
  • Patterns support * wildcards, a trailing / for folders, a leading / for the root, ** for any depth and ! to re-include.
  • Ignoring only affects untracked files — to stop tracking a committed file, use git rm --cached and commit.
  • git check-ignore -v <file> shows exactly which rule ignores a file.
  • Commit .gitignore itself so the whole team shares it; put personal editor and OS files in a global ignore file.