Python
Git
GitHub
.gitignore
Best Practices

Best practices for adding .gitignore file for Python projects?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A good Python .gitignore file does not try to ignore everything that looks temporary. Its job is to keep generated files, local environment state, and secrets out of version control while leaving the real source tree visible and predictable for everyone on the team.

The best starting point is to ignore artifacts that can be recreated, then add project-specific rules carefully. Overly broad ignore rules cause almost as many problems as missing rules.

Start with Common Python Artifacts

Most Python projects should ignore bytecode caches, virtual environments, test caches, and packaging output. A practical baseline looks like this:

gitignore
1__pycache__/
2*.py[cod]
3.pytest_cache/
4.mypy_cache/
5.ruff_cache/
6.venv/
7venv/
8dist/
9build/
10*.egg-info/
11.coverage
12htmlcov/
13.env

This covers the most common files that should not be committed because they are machine-local, generated, or environment-specific.

Ignore the Right Environment Files

Virtual environments almost always belong in .gitignore. They are large, system-specific, and reproducible from dependency declarations.

What you commit instead depends on your tooling:

  • 'requirements.txt'
  • 'poetry.lock and pyproject.toml'
  • 'Pipfile.lock and Pipfile'

The key rule is to commit dependency definitions, not the installed environment directory.

Be Careful with .env and Secrets

Ignoring .env is usually correct because it often contains credentials or machine-specific settings. What you should commit is a safe template such as .env.example with placeholder names only.

That gives other developers a clear setup path without leaking secrets into Git history.

Keep Notebook and IDE Rules Deliberate

If the project uses Jupyter, decide whether notebooks are source artifacts or scratch space. Some teams commit notebooks intentionally. Others ignore checkpoint directories only.

gitignore
.ipynb_checkpoints/
.vscode/
.idea/

Do not blindly ignore every notebook file unless the team has agreed that notebooks are disposable. The same applies to editor folders. Some teams commit selected editor settings, but most local workspace state should stay ignored.

Test Your Rules Instead of Guessing

Git can tell you exactly which rule ignored a file. That is useful when a broad pattern hides something you actually want to commit.

bash
git check-ignore -v .venv/bin/python
git check-ignore -v .env

This is much better than editing .gitignore by guesswork and hoping the next git status looks right.

Avoid Over-Broad Patterns

A bad .gitignore often uses wide patterns like *.log, data/, or config/ without considering what the project actually treats as source. That can hide useful fixtures, sample datasets, or checked-in configuration templates.

Prefer specific patterns with clear intent. Ignore generated content because it is generated, not because its name looks noisy.

Review the Rules as the Project Evolves

A Python project that starts as a small script often grows new tooling such as linters, notebooks, Docker files, or packaging output. Revisit .gitignore when that happens instead of letting stale rules accumulate. The goal is a short, intentional ignore file that matches the current workflow rather than a copied template that nobody understands.

Common Pitfalls

  • Committing virtual environment directories instead of dependency files.
  • Ignoring .env without providing a safe example file.
  • Hiding important project files with overly broad directory or wildcard rules.
  • Treating IDE, notebook, and local config rules as universal when team conventions differ.
  • Editing .gitignore repeatedly without using git check-ignore -v to verify why files are hidden.

Summary

  • A Python .gitignore should exclude generated artifacts, local environment state, and secrets.
  • Commit dependency definitions, not virtual environment directories.
  • Ignore .env, but provide a template such as .env.example.
  • Keep notebook and IDE rules aligned with how the team actually works.
  • Use git check-ignore -v to debug ignore behavior instead of guessing.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions