Gitignore for Python
Ignore generated files without hiding your source.
A small starting point
These patterns cover common Python bytecode, local virtual environments and tool caches. Adapt them to the tools you actually use; this is a suggested starting point, not an official universal template.
__pycache__/
*.py[cod]
.venv/
venv/
.pytest_cache/
.mypy_cache/
.coverage
htmlcov/Keep source and dependency intent
Do not ignore all .py files. Usually commit requirements and project configuration files so another person can set up the project. Whether to commit a lockfile depends on the tool and repository policy.
Ignore is not untrack
Adding a pattern does not remove an already tracked file. First inspect what is tracked. Removing a file from the index while retaining it locally requires a deliberate cached removal and a commit.
git ls-files
git check-ignore -v --no-index PATHSecrets are a different problem
Ignoring a local .env file can reduce accidental additions, but it does not make a secret safe after it was committed. Rotate an exposed secret, then follow the repository cleanup process. Never store real values in a sample .env file.
Quick check
Does a new ignore pattern untrack an existing file?
Choose an answer.
Sources and scope
Command blocks are syntax examples checked against official documentation, not fabricated terminal output. Replace uppercase placeholders with your actual names and inspect status before commands that change work.