This article is part of in the series
Published: Friday 16th May 2025

python git ignore

In the world of Python development, maintaining a clean and efficient Git repository is essential for productive collaboration. One of the most powerful tools for repository hygiene is the humble .gitignore file. This unassuming text file plays a crucial role in determining which files Git should track and which it should ignore. For Python projects, properly configuring your .gitignore can prevent bloated repositories, avoid committing sensitive information, and reduce merge conflicts. This article explores everything you need to know about creating and maintaining effective .gitignore files for Python projects.

Understanding the Basics of .gitignore

A .gitignore file is a plain text file that tells Git which files or directories to ignore in a project. These ignored files won't be tracked by Git, won't appear in git status commands, and won't be added when you use git add . commands.

Why Python Projects Need Special Attention

Python development generates numerous files that shouldn't be tracked in version control:

  • Compiled bytecode files (.pyc, .pyo)
  • Virtual environment directories
  • Package build directories
  • Local configuration files
  • Cached data
  • IDE-specific settings

Without proper gitignore rules, these files can bloat your repository, cause unnecessary merge conflicts, and potentially leak sensitive information.

Creating a .gitignore File for Python Projects

You can create a .gitignore file in the root directory of your repository:

touch .gitignore

Then edit this file with your preferred text editor to add the patterns for files and directories that should be ignored.

Essential Python-Specific Patterns

Here's a starter set of Python-specific patterns that should be in almost every Python project's .gitignore:

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
dist/
build/
*.egg-info/
*.egg

# Virtual environments
venv/
env/
ENV/
.env/
.venv/

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover

# Jupyter Notebook
.ipynb_checkpoints

Let's examine the rationale behind each of these sections:

Byte-compiled Files

__pycache__/
*.py[cod]
*$py.class

Python creates compiled bytecode files for performance optimization. These .pyc, .pyo, and .pyd files are generated automatically and don't need to be version-controlled. The pattern *.py[cod] elegantly matches all three extensions.

C Extensions

*.so

If your project includes C extensions, the compiled shared object files (.so on Unix/Linux) should be ignored as they're platform-specific.

Distribution and Packaging

dist/
build/
*.egg-info/
*.egg

When you build Python packages for distribution, these directories and files are created. Since they are generated from your source code, they shouldn't be committed.

Virtual Environments

venv/
env/
ENV/
.env/
.venv/

Virtual environments contain installed packages and Python binaries specific to your local setup. These large directories should never be committed to version control.

Test and Coverage Reports

htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover

Testing frameworks generate various reports and cache files that don't need version control.

Jupyter Notebooks

.ipynb_checkpoints

Jupyter notebooks create checkpoint files that should be ignored.

Advanced .gitignore Techniques

Beyond the basics, there are several advanced techniques to make your .gitignore even more effective.

Using Global .gitignore Files

If you find yourself adding the same patterns across different projects, consider setting up a global .gitignore:

git config --global core.excludesfile ~/.gitignore_global

Then edit ~/.gitignore_global to include patterns that should be ignored in all your repositories, such as OS-specific files or editor configurations.

Negating Patterns with !

Sometimes you need to ignore a pattern but make exceptions. The exclamation mark lets you negate a pattern:

# Ignore all .log files
*.log

# But track error.log specifically
!error.log

This ignores all .log files except for error.log.

Directory-Specific Rules

You can create .gitignore files in subdirectories to apply rules specifically to those directories:

# Main .gitignore in project root
touch .gitignore

# Subdirectory-specific .gitignore
mkdir tests && touch tests/.gitignore

This is useful for large projects with different requirements for different sections.

IDE and Editor Specific Rules

Different IDEs and editors create their own configuration files and directories. Here are common ones for Python development:

# PyCharm
.idea/
*.iml

# VS Code
.vscode/
*.code-workspace

# Spyder
.spyderproject
.spyproject

# Sublime Text
*.sublime-project
*.sublime-workspace

Consider adding these to your global .gitignore rather than project-specific ones if you consistently use the same development environment.

Environment-Specific Files

Python applications often use configuration files that might differ between environments (development, testing, production). A common approach is to use template files in version control and ignore the actual configuration files:

# Ignore all .env files
*.env

# Add template as an example
!template.env

# Ignore instance configuration
instance/
config.py

This approach lets you keep sensitive information out of your repository while still providing templates for team members to configure their environments.

Database Files

Local databases should generally be ignored:

# SQLite database files
*.sqlite
*.sqlite3
*.db

# Redis dump file
dump.rdb

Handling Generated Files

Python projects might generate various types of files that shouldn't be tracked:

# Generated documentation
docs/_build/
docs/generated/

# Generated files
*.generated.*
*.autogenerated.*

# Log files
logs/
*.log

Common Mistakes and How to Avoid Them

1. Ignoring Files After They're Already Tracked

If you add a file to .gitignore that's already being tracked, Git will continue to track it. To stop tracking a file that's already committed:

git rm --cached filename

Or for multiple files:

git rm --cached `git ls-files -i --exclude-from=.gitignore`

Always set up your .gitignore early in your project's lifecycle.

2. Not Checking Generated .gitignore Files

When using tools or templates to generate .gitignore files, always review them to ensure they match your project's needs. Some generated files might be too restrictive or not restrictive enough.

3. Neglecting to Update .gitignore as the Project Evolves

As your project grows and changes, your .gitignore should evolve with it. Review and update it regularly, especially when:

  • Adding new dependencies
  • Switching development tools
  • Implementing new build processes
  • Adding new file formats or assets

Templates and Resources

GitHub's Python .gitignore Template

GitHub maintains a comprehensive collection of .gitignore templates, including one specifically for Python projects: github.com/github/gitignore/blob/master/Python.gitignore

You can use this template when creating a new repository on GitHub, or download it manually:

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore

gitignore.io

gitignore.io is a web service that generates .gitignore files based on your project's technologies. For a Python project using PyCharm and Django, you could visit:

https://www.toptal.com/developers/gitignore/api/python,pycharm,django

Interactive Tool - gi CLI

For command-line enthusiasts, gi is a command-line tool for generating .gitignore files:

# Install gi
pip install gi

# Generate a Python .gitignore
gi python > .gitignore

# Add more technologies
gi python,django,vscode >> .gitignore

Framework-Specific Considerations

Different Python frameworks have unique files and directories that should be ignored. Here are some common additions for popular frameworks:

Django

# Django specific
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
media/
staticfiles/

# Django migrations (sometimes ignored in development)
# */migrations/*.py
# !*/migrations/__init__.py

Flask

# Flask instance folder
instance/

# Flask file uploads
uploads/

# Flask environment variables
.env
.flaskenv

Jupyter/Data Science

# Jupyter
.ipynb_checkpoints
*/.ipynb_checkpoints/*

# IPython
profile_default/
ipython_config.py

# Data files (sometimes too large for Git)
*.csv
*.dat
*.out

Best Practices for Team Collaboration

When working in a team, consistent .gitignore practices are essential:

  1. Document Ignored Patterns: Add comments to your .gitignore explaining why certain patterns are ignored.
  2. Communicate Changes: When modifying .gitignore, communicate the changes to your team, especially if they need to recreate ignored files locally.
  3. Keep It in Version Control: The .gitignore file itself should be committed to your repository so everyone shares the same rules.
  4. Be Careful with Wildcards: Overly broad patterns can accidentally ignore important files.
  5. Use Multiple .gitignore Files: For complex projects, consider using directory-specific .gitignore files to maintain clarity.

Maintaining Your .gitignore Over Time

A .gitignore file isn't a set-it-and-forget-it resource. As your project evolves, so should your ignore rules:

  1. Periodic Audits: Regularly review your .gitignore file to ensure it's still appropriate.
  2. Check for Untracked Files: Use git status --ignored to see what files are being ignored and verify this matches your expectations.
  3. Look for Patterns: If you frequently find yourself manually excluding certain files, add a pattern to .gitignore.
  4. Clean Up Obsolete Rules: Remove patterns for technologies or tools you no longer use.

.gitignore vs. Other Exclusion Methods

Git offers several ways to ignore files, each with different use cases:

  1. Repository .gitignore: For project-wide rules that should be shared with all contributors.
  2. Global .gitignore: For personal preferences that apply across all your repositories.
  3. .git/info/exclude: For personal, repository-specific exclusions that shouldn't be shared.
  4. Explicitly Ignored Paths: For one-off ignores, use git add -A -- :!path/to/ignore.

Security Considerations

The .gitignore file plays an important role in security:

  1. Always Ignore Sensitive Files: Configuration files with secrets, API keys, passwords, or personal credentials should always be ignored.
  2. Use Environment Variables: Instead of configuration files with hardcoded secrets, use environment variables and ignored .env files.
  3. Check Before Committing: Before committing, use git diff --cached to review what's being committed.
  4. Scan for Secrets: Consider using pre-commit hooks or tools like git-secrets to prevent accidentally committing sensitive information.

More Articles from Python Central

pytesseract in Python: How to Build OCR Function

Python ++: Understanding this Increment Operator

Using Python to Track International Shipments: Dealing with Multiple Carriers