Ignoring Files with .gitignore

A .gitignore file tells Git which files and folders to leave untracked.

Some files should never be committed: dependency folders, build output, logs, and secrets. List their patterns in a file named .gitignore at the root of your repo.

Pattern rules

  • node_modules/ — ignore an entire folder.
  • *.log — ignore all files with the .log extension.
  • !keep.log — an exception; do not ignore this file.
  • .env — ignore a specific file (great for secrets).

GitHub provides ready-made templates at github/gitignore for most languages.

Example

Example · bash
# .gitignore
node_modules/
dist/
*.log
.env
.DS_Store

When to use it

  • A Python developer adds __pycache__/ and *.pyc to .gitignore so compiled bytecode is never accidentally committed.
  • A Node.js team ignores node_modules/ to keep the repository lean; dependencies are restored from package.json instead.
  • A developer adds .env to .gitignore before the first commit, preventing API keys from ever reaching GitHub.

More examples

Basic .gitignore for Python project

A minimal .gitignore that keeps Python artefacts, the virtual environment, and secrets out of version control.

Example · bash
cat .gitignore
# Byte-compiled files
__pycache__/
*.pyc

# Virtual environment
.venv/

# Secrets
.env

Untrack an already-committed file

git rm --cached removes the file from the index without deleting it on disk, then .gitignore keeps it out forever.

Example · bash
# Remove from index without deleting on disk
git rm --cached secrets.env

# Add to .gitignore so it stays ignored
echo 'secrets.env' >> .gitignore
git commit -m "Remove secrets.env from tracking"

Debug why a file is ignored

Reports the exact line and pattern in .gitignore responsible for ignoring the file, useful when a file unexpectedly disappears from git status.

Example · bash
git check-ignore -v dist/bundle.js
# .gitignore:4:dist/    dist/bundle.js

Discussion

  • Be the first to comment on this lesson.
Ignoring Files with .gitignore — GitHub | SoundsCode