Django: Which files should be added to ‘.gitignore’?

Updated: January 21, 2024 By: Guest Contributor Post a comment

Introduction

Version control is essential in any development project and using Git is a common practice. But not every file in a Django project should be tracked. This is where a .gitignore file becomes necessary. Because certain files may contain sensitive data, such as API keys or database credentials, or simply are unnecessary for version control, like database files, understanding what goes into a .gitignore for a Django project will protect your secrets and keep your repository clean.

Let’s dive into the details of what your .gitignore should typically include in a Django project, along with how to generate or modify this file to secure and streamline your development workflow.

Understanding the .gitignore File

A .gitignore file is a text file that tells Git which files or directories to ignore in a project. Usually, these are files that don’t need to be shared with others such as compiled code, temporary files created by the operating system, or personal IDE settings.

Files Should be Ignored in Git

In Django, the .gitignore file is particularly important for several reasons:

  • Privacy: To not expose sensitive configuration settings.
  • Cleanliness: To not crowd your repository with unnecessary or generated files.

The following sections detail what should generally be included in a Django project’s .gitignore.

1. Virtual Environment Folder

venv/
.env/

The virtual environment directory contains all of the Python packages that you’ve installed for your project. These should be ignored because they are specific to your local setup and can cause conflicts with other developers’ environments.

2. Compiled Python Files

__pycache__/
*.pyc

Compiled Python files are platform-specific and can easily be re-generated by running the Python interpreter. Since they provide no additional information beyond what’s in your source code, it’s best to leave them out of your repository.

3. Database Files

*.sqlite3
*.db

SQLite database files should be ignored because they may contain data that is not intended for sharing or could differ from production databases. For other types of databases, you may need to ignore additional files (like MySQL files such as *.frm, *.log, and *.ibd files).

4. Media and Static Files

media/
staticfiles/

If you’re uploading media from users or using staticfiles to manage your static file serving, you’ll want to exclude these directories. These files are usually managed separately using a Content Delivery Network (CDN) or similar services in production.

5. Secret Keys and Credentials

settings_local.py

A local settings file might contain overrides for your settings.py file that include secret keys and database credentials. These are highly sensitive and should strictly be kept private.

6. IDE/Text Editor Configurations

.idea/
*.sublime-
*.swp
*.swo

IDE or editor configurations are personal preferences, and there’s no need to share them with others. They can also interfere with the configurations of fellow developers working on the same project.

Additional Considerations

Beyond the essentials, depending on the tools and methodologies you are using, the following might also need to be added:

  • Log files generated during development or by web servers.
  • Local test configurations and directories.
  • Coverage reports from tools like Coverage.py.
  • Build directories for front-end frameworks such as ReactJS, VueJS, or Angular.
  • Dependency lock files other than Pipfile.lock or requirements.txt.

Automatically Generating a .gitignore File

If manually curating a list seems overwhelming, automated tools can help generate a .gitignore file. Such as:

  • Gitignore.io: A web service that generates files for many different environments.
  • gitignore templates from GitHub: Curated defaults that GitHub offers for different project types.

To further customize your .gitignore file, individual ignores can be added or rules can be combined. Comment lines can start with a # to provide clarity:

# Custom log files
*.log

# OS generated files
.DS_Store
Thumbs.db

# Package directories
node_modules/

Remember, once a file has been tracked by Git, adding it to a .gitignore will not remove it from the repository. You’ll need to explicitly remove them or use a command like git rm --cached to untrack the file while keeping it in your directory.

Conclusion

Securing and cleaning up your Django repository with an appropriate .gitignore file is important. This guide should help you understand what you typically need to include, but always consider the specifics of your own project and adjust accordingly. The .gitignore file is a tool to help keep your project tidy and secure, so use it to suit the project’s needs expertly. And don’t forget, when in doubt, err on the side of ignoring something questionable—you can always change it later.