Script Valley
Git and GitHub Complete Course: From Beginner to Advanced
Introduction to Version Control and Git BasicsLesson 1.5

Ignoring Files with .gitignore

.gitignore, ignore patterns, node_modules, build artifacts, environment variables, .env files

Ignoring Files with .gitignore

Not every file in your project should be tracked by Git. Build artifacts, dependency folders, log files, and sensitive configuration should all be excluded. The .gitignore file tells Git which files and directories to ignore.

Diagram.gitignore Pattern Rules

IMAGE PROMPT (replace this block with your generated image):

Flat annotated file-tree diagram on white background. Title: How .gitignore Works. Left side: a project folder tree showing: my-project/ containing src/, node_modules/ (red X overlay — ignored), .env (red X — ignored), dist/ (red X — ignored), .gitignore (green — tracked), README.md (green — tracked), index.js (green — tracked). Right side: a .gitignore file box (fill: #f8f8f8, monospace font) showing the file contents with color-coded lines: Gray comment lines (# Node.js, # Build, # Env). Red-highlighted pattern lines: node_modules/, dist/, build/, .env, *.log. Callout arrows in #3A5EFF connecting each pattern in the file to the corresponding red-X item in the folder tree. Three annotation bubbles: / suffix = directory, * = wildcard, # = comment. Bottom badge: git rm --cached file — untrack an already-committed file. White background.

Creating a .gitignore File

Create a file named .gitignore in the root of your repository. Add patterns for files and folders you want Git to ignore:

# Node.js dependencies
node_modules/

# Build output
dist/
build/

# Environment variables
.env
.env.local

# OS-generated files
.DS_Store
Thumbs.db

# Log files
*.log

Patterns ending with / match directories. Patterns starting with ! are negations (include a file that a broader pattern would ignore). The * wildcard matches any sequence of characters within a filename.

Global .gitignore

You can set a global ignore file for system-level files that should be ignored in every project:

git config --global core.excludesfile ~/.gitignore_global

Add entries like .DS_Store and *.swp to this file so you never accidentally commit OS or editor artifacts.

Untracking Already-Tracked Files

If you added a file to .gitignore after already committing it, Git will still track it. Remove it from tracking without deleting it from disk:

git rm --cached filename.txt

Then commit the change. The file will no longer appear in future commits.