If you are using Git for version control, you need a Gitignore file to ignore all files that don’t matter and shouldn’t be in your git repository. Think of your virtual environment and all the .pyc files. Those are both generated and can be generated by anyone that has access to your code. Therefore, it’s unnecessary to add those to your repository.
There are a lot of different file types and specific folders that you don’t need. Even outside of Django. Think about your personal settings in VS Code (if you use that). Here is a list which covers all things that you can ignore through gitignore for every Django project you start. Put this list in the root of your Django project and call it
.gitignore (yes, with the dot!).
Here’s the ultimate Django gitignore
What we are ignoring and not with this Django .gitignore
A common question is: “why aren’t we ignoring migrations?”. Wouldn’t it make sense to create the migrations on the server and then migrate them right away? Yes, in some cases that would make sense, but there is a big issue with that. You will always want to have the same migrations on your development machine as on the server. Imagine this: you want a field to be
False for every record. You create a new field with
default=False, in this case all fields are fields. Then you decide to change the field and change the
True. The current fields are still marked as
False since you ran that migration first. If you wouldn’t commit the migration files, all fields would be
True since the production server wouldn’t know that it was
Next to that, Django recommends including migration files as they are part of the code base:
The migration files for each app live in a “migrations” directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleagues’ machines, your staging machines, and eventually your production machines.
Python will always compile your code to byte code. This is saved in the .pyc files. You can’t do much with that and we don’t need it, python will create them anyway. It’s best to just ignore them through .gitignore.