Git is the most popular distributed version control system, and you are probably already using it for your Django project. Although you are tracking file changes for most of your files, it's recommended that you keep some specific files and folders out of version control. Usually, caches, compiled code, log files, and hidden system files should not be tracked in the Git repository.
Creating the Git ignore file
Getting ready
Make sure that your Django project is under Git version control.
How to do it...
Using your favorite text editor, create a .gitignore file at the root of your Django project and put the following files and directories there:
# .gitignore
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
db.sqlite3
# Sphinx documentation
docs/_build/
# IPython
profile_default/
ipython_config.py
# Environments
env/
# Media and Static directories
/media/
!/media/.gitkeep
/static/
!/static/.gitkeep
# Secrets
secrets.json
How it works...
The .gitignore file specifies patterns that should intentionally be untracked by the Git version control system. The .gitignore file that we created in this recipe will ignore the Python-compiled files, local settings, collected static files, and media directory with the uploaded files.
Note that we have exceptional syntax with exclamation marks for media and static files:
/media/
!/media/.gitkeep
This tells Git to ignore the /media/ directory but keep the /media/.gitkeep file tracked under version control. As Git version control tracks files, but not directories, we use .gitkeep to make sure that the media directory will be created in each environment, but not tracked.
See also
- The Creating a project file structure recipe
- The Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL recipe