8 minlesson

Ignoring Files with .gitignore

Ignoring Files with .gitignore

Not everything belongs in version control. Build artifacts, dependencies, secrets, and editor configurations should be excluded. The .gitignore file tells Git what to ignore.

Why Ignore Files?

Tracking the wrong files causes problems:

  • Dependencies (node_modules/, vendor/): Bloats repository size, slow clones
  • Build output (dist/, *.o): Generated files that can be rebuilt
  • Secrets (.env, *.pem): Security risk if pushed
  • Editor files (.idea/, .vscode/): Personal preferences shouldn't be shared
  • OS files (.DS_Store, Thumbs.db): System-specific clutter

Creating .gitignore

Create a .gitignore file in your repository root:

bash
1$ cat <<'EOF' > .gitignore
2# Dependencies
3node_modules/
4vendor/
5
6# Build output
7dist/
8build/
9*.o
10*.pyc
11__pycache__/
12
13# Environment & secrets
14.env
15.env.local
16*.pem
17
18# Editor & OS files
19.DS_Store
20.idea/
21.vscode/
22*.swp
23EOF

Pattern Syntax

Basic Patterns

PatternMatches
file.txtfile.txt in any directory
/file.txtfile.txt only in root
dir/Directory and all contents
*.logAll .log files
!important.logException: track this file

Wildcards

WildcardMeaning
*Any characters except /
**Any directories
?Single character
[abc]One of a, b, or c

Examples

gitignore
1# All .log files anywhere
2*.log
3
4# All .log files in logs/ directory only
5logs/*.log
6
7# All files in any build directory
8**/build/
9
10# .env in root, but not in subdirectories
11/.env
12
13# Ignore all .txt except readme.txt
14*.txt
15!readme.txt

Common Templates

Node.js

gitignore
1node_modules/
2npm-debug.log*
3.env
4dist/
5coverage/

Python

gitignore
1__pycache__/
2*.py[cod]
3.env
4venv/
5.pytest_cache/

Java

gitignore
1*.class
2*.jar
3target/
4.idea/
5*.iml

General

gitignore
1# OS
2.DS_Store
3Thumbs.db
4
5# Editors
6.idea/
7.vscode/
8*.swp
9*~
10
11# Secrets
12.env
13.env.*
14*.pem
15*.key

Global Gitignore

For patterns that apply to all your repositories (like editor files), create a global gitignore:

bash
1$ git config --global core.excludesFile ~/.gitignore_global
2$ echo ".DS_Store" >> ~/.gitignore_global
3$ echo ".idea/" >> ~/.gitignore_global

Already Tracked Files

.gitignore only affects untracked files. If a file is already tracked:

bash
1# Remove from tracking but keep local file
2$ git rm --cached secret.env
3$ echo "secret.env" >> .gitignore
4$ git commit -m "chore: stop tracking secret.env"

The --cached flag removes from Git but keeps the file on disk.

Checking What's Ignored

See which files are being ignored:

bash
1$ git status --ignored

Check if a specific file would be ignored:

bash
1$ git check-ignore -v debug.log
2.gitignore:3:*.log debug.log

Template Resources

GitHub maintains language-specific templates:

Generate templates online:

Key Takeaway

A well-crafted .gitignore keeps your repository clean and secure. Ignore dependencies, build outputs, secrets, and editor configurations. Use global gitignore for personal preferences. Remember that .gitignore doesn't affect already-tracked files.