01 / 20
.gitignore

Using .gitignore

Learn how to exclude unnecessary files from your Git repositories: virtual environments, build artifacts, and more.

Python

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 not be tracked, staged, or committed.

Why Does .gitignore Matter?

★ Smaller Repos
Virtual environments can contain thousands of files. Ignoring them keeps your repo lightweight and fast to clone.
★ Cleaner History
Build artifacts change every compile. Tracking them pollutes your commit history with meaningless diffs.
★ Security
Credentials, API keys, and .env files should never be committed. A .gitignore prevents accidental exposure.

Common Files to Exclude

Your Repository venv/ __pycache__/ *.pyc dist/ / build/ .env ✕ app.py requirements.txt README.md .gitignore ✓ IGNORED TRACKED

Core Pattern Syntax

The .gitignore file uses glob-style patterns to match files and directories. Each line specifies one pattern.

* Wildcard
Matches any number of characters except /. Example: *.pyc matches all .pyc files.
/ Directory
A trailing / means "directories only." Example: venv/ ignores the folder, not a file named venv.
! Negation
Prefixing with ! re-includes a previously excluded file. Example: !important.log
# Comment
Lines starting with # are comments. Use them to document why each pattern exists.

The Golden Rule

1 File created Untracked 2a .gitignore blocks 2b Already committed 3 git rm --cached Then .gitignore works

venv/ vs __pycache__/

venv/ __pycache__/
What is it? Python virtual environment Compiled bytecode cache
Contains Python interpreter + all packages .pyc files (auto-generated)
Typical size 50 MB to 500+ MB A few KB per module
Reproducible? Yes, via pip install -r requirements.txt Yes, auto-regenerated on import
Must ignore? Absolutely Absolutely

Build Artifacts to Exclude

1
dist/ and build/: Directories generated when you package your project with setuptools or build tools.
2
*.egg-info/: Metadata created during pip install -e . (editable installs).
3
*.egg: The legacy Python distribution format. Always regenerated on build.
4
.tox/ and .nox/: Test runner environments. Huge and fully reproducible.
5
.coverage and htmlcov/: Code coverage reports. Generated on each test run.

Step-by-Step Setup

1
Create the file in your project root: name it exactly .gitignore (note the leading dot).
2
Add patterns line by line: one pattern per line. Use comments with # to explain each section.
3
Commit the .gitignore itself. Yes, the .gitignore file should be tracked so all collaborators share the same rules.
4
Verify with git status: ignored files should not appear. Use git check-ignore -v <file> to debug patterns.

Example 1: Minimal Python .gitignore

# Virtual environments venv/ env/ .venv/ # Bytecode __pycache__/ *.pyc # Environment variables .env

Example 2: Adding Build Artifacts

# Build & distribution build/ dist/ *.egg-info/ *.egg # Testing .tox/ .coverage htmlcov/ # IDE & OS .idea/ .vscode/ .DS_Store Thumbs.db

Example 3: Negation Patterns

# Ignore all log files *.log # But keep the important one !important.log # Ignore everything in /data data/ # But track the README inside it !data/README.md

Seeing It In Action

my-project/ ├── app.py ├── utils.py ├── requirements.txt ├── .gitignore ├── README.md ├── venv/ ← IGNORED ├── __pycache__/ ← IGNORED ├── .env ← IGNORED ├── dist/ ← IGNORED └── build/ ← IGNORED

Advanced Wildcards

Pattern Matches Does Not Match
*.pyc module.pyc, test.pyc module.py
logs/ logs/ directory anywhere a file named "logs"
/debug.log debug.log at root only src/debug.log
**/*.tmp .tmp files in any subdirectory files without .tmp extension
doc/**/*.pdf doc/a.pdf, doc/v1/b.pdf notes/c.pdf

Common Mistakes

✕ Ignoring after committing
Adding a pattern after the file is already tracked does nothing. You must git rm --cached first.
✕ Forgetting the trailing /
Writing venv instead of venv/ can match a file named "venv" as well as the directory. Be explicit.
✕ Negating inside ignored dirs
You cannot use ! to re-include a file if its parent directory is wholly ignored. Ignore contents with dir/* instead.

Summary Cheat Sheet

Category Patterns Why Ignore?
Virtual Environments venv/, env/, .venv/ Huge, reproducible via pip
Bytecode __pycache__/, *.pyc Auto-generated on import
Build Artifacts build/, dist/, *.egg-info/ Regenerated on build
Secrets .env, *.key Security risk
IDE / OS .idea/, .vscode/, .DS_Store Machine-specific

Question 1 of 5

What is the primary purpose of a .gitignore file?

Question 2 of 5

You added venv/ to your .gitignore, but the venv directory still appears in git status. What is the most likely reason?

Question 3 of 5

Which pattern would ignore all .pyc files in every subdirectory of your project?

Question 4 of 5

Your .gitignore contains these two lines:
*.log
!error.log

Which statement is correct?

Question 5 of 5

Why should you always commit the .gitignore file itself to the repository?