01 / 21

Using .gitignore

Learn how to exclude files and folders from version control to keep your repositories clean, fast, and secure.

Programming

What is .gitignore?

A .gitignore file is a plain text file placed at the root of your Git repository. It tells Git which files and directories to completely ignore: they will never be tracked, staged, or committed.

Why Ignore Files?

Repository Size
node_modules can contain 200+ MB of dependencies. Tracking them bloats your repo and slows cloning.
Reproducibility
Build artifacts like dist/ can be regenerated from source. Committing them creates merge conflicts for no benefit.
Security
Files like .env contain API keys and passwords. Pushing them to a public repo exposes sensitive data.

File States in Git

Tracked index.js package.json README.md .gitignore Git watches every change ✓ Untracked newfile.js notes.txt Git sees them but doesn't track yet ? Ignored node_modules/ dist/ .env Git pretends they don't exist ✕

The Core Rule

Each line in a .gitignore file specifies a pattern. Any file or directory whose path matches a pattern is excluded from version control.

.gitignore node_modules/ dist/ .env *.log matches Excluded node_modules/ dist/ .env error.log, debug.log

Pattern Syntax Basics

1
Literal names: writing secret.key ignores any file named exactly secret.key.
2
Wildcards (*): the * matches any characters except /. Example: *.log ignores all .log files.
3
Directories (/): a trailing slash like dist/ targets only directories, not files named dist.
4
Negation (!): prefix a pattern with ! to re-include something previously ignored. Example: !dist/keep.txt
5
Comments (#): lines starting with # are comments and are ignored by Git.

Glob Patterns vs Directory Patterns

Pattern What It Matches Type
*.log All files ending in .log at any depth Glob
build/ Only directories named build Directory
build Files AND directories named build Glob
**/temp temp at any directory depth Glob
!important.log Re-includes important.log Negation

Why Ignore node_modules?

my-project/ src/ (your code) ~50 KB package.json ~2 KB node_modules/ 200+ MB 50,000+ files regenerated by npm install npm install ✓ ✕ package.json records what to install. node_modules is the result. Commit the recipe, not the ingredients.

What Are Build Artifacts?

Build artifacts are files generated by tools from your source code. They can always be recreated, so storing them in Git is redundant and causes unnecessary merge conflicts.

dist/
Bundled, minified output from Webpack, Vite, or Parcel
build/
Compiled output from TypeScript, Babel, or frameworks like React
coverage/
Test coverage reports generated by Jest or other testing tools

Creating a .gitignore Step by Step

1
Create the file in your project root: name it exactly .gitignore (note the leading dot).
2
Add patterns one per line for files/folders you want excluded.
3
Verify with git status: the ignored files should not appear in the output.
4
Commit .gitignore itself so all team members share the same ignore rules.
5
If files are already tracked, run git rm --cached <file> first to untrack them before ignoring.

Example 1: A Basic .gitignore

File: .gitignore
# Dependencies node_modules/ # Build output dist/ # Environment variables .env # OS-generated files .DS_Store Thumbs.db

Example 2: Wildcards and Negation

File: .gitignore
# Ignore all log files *.log # But keep the error log for debugging !error.log # Ignore all .tmp files in any subdirectory **/*.tmp # Ignore the entire build folder build/ # But keep the README inside build !build/README.md

Example 3: Full Project .gitignore

File: .gitignore (production-ready)
# Dependencies node_modules/ # Build artifacts dist/ build/ .next/ coverage/ # Environment and secrets .env .env.local .env.production # Logs *.log logs/ # IDE and OS .vscode/ .idea/ .DS_Store

Without .gitignore

With .gitignore src/index.js package.json README.md .gitignore 4 files ~100 KB total Clone: 2 seconds Without .gitignore src/index.js package.json node_modules/ (50K files) dist/ (bundled output) .env (API keys exposed!) 50,000+ files ~300 MB, Clone: 5+ min

Common Mistakes

✕ Adding .gitignore Too Late
If files are already tracked, adding them to .gitignore won't untrack them. You must first run git rm --cached.
✕ Forgetting the Trailing Slash
Writing dist instead of dist/ may also match a file named "dist". Always use the slash for directories.
✕ Ignoring package.json
Some students ignore everything in the root. package.json and package-lock.json must be committed: they define your dependencies.

What to Ignore vs What to Commit

File / Folder Ignore Commit Why
node_modules/ ✕ Regenerated by npm install
dist/ or build/ ✕ Regenerated by build command
.env ✕ Contains secrets
*.log ✕ Runtime noise, not source
package.json ✓ Defines project dependencies
src/ ✓ Your actual source code
.gitignore ✓ Shared ignore rules

Question 1 of 5

What is the primary purpose of a .gitignore file?

Question 2 of 5

Which of the following should always be listed in a JavaScript project's .gitignore?

Question 3 of 5

What does the pattern *.log do in a .gitignore file?

Question 4 of 5

You add node_modules/ to your .gitignore, but Git is still tracking the directory. What is the most likely reason?

Question 5 of 5

Given this .gitignore content:
*.log
!error.log

Which files will be ignored?