CPCODELAB
Git & GitHub

Ignoring Files with .gitignore

6 min read

What Should You Ignore?

Not every file in your project should be tracked. Build artefacts, dependency folders, and environment files with secrets should all be excluded. The .gitignore file tells Git which paths to leave alone.

text
# Dependencies
node_modules/

# Environment secrets — NEVER commit these
.env
.env.local
.env.*.local

# Build output
dist/
.next/
out/

# OS files
.DS_Store
Thumbs.db

# Editor
.vscode/settings.json
*.log

Patterns use glob syntax: * matches any file name, **/ matches any directory level, a trailing / matches directories only, and a leading ! negates the rule.

Already-Tracked Files

Adding a path to .gitignore only works for untracked files. If a file was already committed, Git keeps tracking it. To stop tracking it:

bash
git rm --cached .env
git commit -m 'stop tracking .env'

Committing .env files or API keys is a serious security incident. Even after removal, secrets remain in git history and should be rotated immediately. Use GitHub's secret scanning alerts as a safety net, but prevention is better.

Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises