Using .gitignore
Learn how to exclude files and folders from version control to keep your repositories clean, fast, and secure.
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?
File States in Git
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.
Pattern Syntax Basics
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?
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.
Creating a .gitignore Step by Step
Example 1: A Basic .gitignore
Example 2: Wildcards and Negation
Example 3: Full Project .gitignore
Without .gitignore
Common Mistakes
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?