How to Add a Django Project to GitHub: Step-by-Step Guide

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:


๐Ÿ”ง 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

  1. Log in to GitHub.
  2. Click the “+” icon > New repository.
  3. Name your repo (e.g., my-django-app) and optionally add a description.
  4. Leave โ€œInitialize this repository with a READMEโ€ unchecked.
  5. 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, use git 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:

  1. Add .env to .gitignore
  2. 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

StepDescription
1Initialize Git with git init
2Add .gitignore for security and cleanup
3Commit your project files
4Create a GitHub repository
5Connect and push using Git
6Protect environment variables
7Verify 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.

Sharing Is Caring:

Leave a Comment