Version control is a cornerstone of modern software development, and GitHub is the most popular platform for hosting Git repositories. If you’re working on a Django web application and want to share it, collaborate with others, or simply back it up, pushing your project to GitHub is a smart move.
In this tutorial, youโll learn how to add a Django project to GitHub from start to finish, including best practices for .gitignore
and environment security.
๐ฆ Prerequisites
Before you begin, make sure you have:
- A GitHub account: https://github.com
- Git installed on your machine: Download Git
- A Django project ready on your local machine
๐ง Step 1: Initialize Git in Your Django Project Directory
Open your terminal or Git Bash and navigate to your project folder:
cd path/to/your/django-project
Initialize a Git repository:
git init
This will create a .git
directory and allow Git to track your files.
๐ Step 2: Create a .gitignore
File
A .gitignore
file tells Git which files and folders to ignore (e.g., sensitive info, compiled files, virtual environments).
Create a file named .gitignore
in your project root and add:
# Python
*.pyc
__pycache__/
# Virtual environments
venv/
env/
# Django-specific
*.sqlite3
*.log
db.sqlite3
# Static files
staticfiles/
media/
# Environment variables
.env
# VSCode and IDE settings
.vscode/
.idea/
# Migrations (optional, depending on workflow)
*/migrations/__pycache__/
*/migrations/*.pyc
You can also use a pre-built template: https://github.com/github/gitignore/blob/main/Python.gitignore
๐๏ธ Step 3: Stage and Commit Your Files
Tell Git which files to track:
git add .
Then commit your changes:
git commit -m "Initial commit: Django project setup"
๐ Step 4: Create a Repository on GitHub
- Log in to GitHub.
- Click the “+” icon > New repository.
- Name your repo (e.g.,
my-django-app
) and optionally add a description. - Leave โInitialize this repository with a READMEโ unchecked.
- Click Create repository.
๐ Step 5: Connect Your Local Repo to GitHub
Copy the repositoryโs URL (either HTTPS or SSH), then run:
git remote add origin https://github.com/your-username/your-repo-name.git
Push your code:
git push -u origin master
๐ If your branch is
main
, usegit push -u origin main
instead.
๐ก๏ธ Step 6: Protect Sensitive Data
Make sure not to upload any sensitive information like:
- Secret keys (
SECRET_KEY
) - Database credentials
- API keys
Use an .env
file and Django-environ or python-decouple
to manage your environment variables. Donโt forget to:
- Add
.env
to.gitignore
- Load the environment variables in
settings.py
Example using python-decouple
:
from decouple import config
SECRET_KEY = config('SECRET_KEY')
โ Step 7: Verify Everything is Uploaded
Check your repository on GitHub to make sure:
- Files are properly structured
- Sensitive files are ignored
- README,
.gitignore
, and license are present if needed
Bonus: Optional Enhancements
- Add a
README.md
with project details and setup instructions - Add a license (e.g., MIT or Apache 2.0)
- Create a requirements file:
pip freeze > requirements.txt
- Consider adding tests and CI/CD (e.g., GitHub Actions)
๐ Summary
Step | Description |
---|---|
1 | Initialize Git with git init |
2 | Add .gitignore for security and cleanup |
3 | Commit your project files |
4 | Create a GitHub repository |
5 | Connect and push using Git |
6 | Protect environment variables |
7 | Verify repo contents |
Pushing your Django project to GitHub is a foundational skill for any developer. It improves collaboration, encourages clean project structure, and enables powerful integrations with CI/CD, deployment tools, and open-source communities.