GitHub for Django - potatoscript/django GitHub Wiki

Using Git and GitHub with Django 🚀

Git and GitHub are essential tools for version control and collaboration in software development. Using Git with Django allows you to manage and track changes in your project efficiently, collaborate with team members, and keep your code safe by pushing it to a remote repository on GitHub.

In this tutorial, we’ll walk through the basics of using Git and GitHub with your Django project, from initializing a Git repository to pushing your project to GitHub.


🧩 Part 1: Setting Up Git with Django

🛠️ Step 1: Install Git

Before we begin, make sure you have Git installed on your system. You can check if you have Git installed by running:

git --version

If Git isn’t installed, download and install it from git-scm.com.


🛠️ Step 2: Initialize a Git Repository

  1. Navigate to Your Django Project Folder:

    In your terminal, go to the root directory of your Django project. For example:

    cd path/to/your/django-project
    
  2. Initialize the Git Repository:

    Run the following command to initialize a Git repository in your project directory:

    git init
    

    This will create a .git directory that will hold all the version control data for your project.


🛠️ Step 3: Add Files to Git

  1. Track Files:

    Git doesn’t automatically track files, so you need to add the files you want to version control. You can add all the files in your project by running:

    git add .
    

    This will add all files to the staging area, except for files listed in .gitignore (we’ll set that up next).

  2. Commit Changes:

    After adding the files, commit them to the repository:

    git commit -m "Initial commit of Django project"
    

    This command saves a snapshot of your project at this point in time. The -m flag allows you to add a commit message.


🧩 Part 2: Setting Up a GitHub Repository

Now that you’ve initialized your Git repository and committed your changes locally, let’s create a remote repository on GitHub.

  1. Create a GitHub Account:

    If you don’t already have a GitHub account, sign up at GitHub.

  2. Create a New Repository:

    • Go to your GitHub homepage and click the New repository button.
    • Give your repository a name, e.g., my-django-project.
    • You can choose to make it public or private.
    • Don’t initialize the repository with a README, license, or .gitignore (we’ll set those up locally).
    • Click Create repository.

🧩 Part 3: Pushing Your Django Project to GitHub

Now that you have your GitHub repository ready, let’s link your local Git repository to the remote one on GitHub.

  1. Add Remote Origin:

    Copy the URL of your GitHub repository (it should look like https://github.com/username/my-django-project.git) and add it as a remote origin:

    git remote add origin https://github.com/username/my-django-project.git
    
  2. Push to GitHub:

    Finally, push your local commits to the remote GitHub repository:

    git push -u origin master
    

    This will upload your local code to GitHub. After this, you can visit your GitHub repository and see the code live!


🧩 Part 4: Setting Up .gitignore

In most projects, there are certain files and folders that you don’t want to track in Git, like temporary files, virtual environments, or database files. This is where the .gitignore file comes in.

  1. Create a .gitignore File:

    In the root of your Django project, create a file named .gitignore and open it for editing.

  2. Add Common Django and Python Files to .gitignore:

    Here’s a basic .gitignore template for Django:

    # Python bytecode files
    __pycache__/
    *.py[cod]
    
    # Django files
    db.sqlite3
    /static/
    /media/
    
    # Virtual Environment
    venv/
    .env/
    
    # IDE configurations (example for VS Code)
    .vscode/
    
    # OS-specific files
    .DS_Store
    Thumbs.db
    
    • This will ensure that files like the SQLite database (db.sqlite3), static files (/static/), virtual environment folders (venv/), and OS-specific files like .DS_Store are ignored by Git.
  3. Add .gitignore to Git:

    After creating the .gitignore file, add it to Git and commit the changes:

    git add .gitignore
    git commit -m "Add .gitignore"
    

🧩 Part 5: Working with Branches

Branches allow you to work on new features or bug fixes without affecting the main codebase. Here’s how to create and work with branches:

  1. Create a New Branch:

    To create a new branch, use the following command:

    git checkout -b feature-branch
    

    This creates a new branch called feature-branch and switches to it.

  2. Commit Changes to the Branch:

    Make your changes to the code, then commit them:

    git add .
    git commit -m "Add feature X"
    
  3. Push the Branch to GitHub:

    To push the branch to GitHub, use:

    git push origin feature-branch
    
  4. Create a Pull Request:

    Go to your GitHub repository, and you’ll see an option to create a Pull Request. This allows you to review and merge your changes into the master (or main) branch.


🧩 Part 6: Collaborating on GitHub

When working on a team, collaboration becomes crucial. Here are a few key things to know:

  1. Pulling Changes:

    If someone else has pushed changes to the repository, you can pull those changes by running:

    git pull origin master
    
  2. Resolving Merge Conflicts:

    Sometimes, when two people make changes to the same line of code, Git can’t automatically merge them. You’ll need to manually resolve the conflict by editing the file and then committing the changes.


🧩 Part 7: Updating Your Django App on GitHub

Whenever you make changes to your Django project, you’ll want to push those changes to GitHub to keep everything up to date.

  1. Add Changes:

    After making changes to your Django app, add the updated files:

    git add .
    
  2. Commit Changes:

    Commit the changes with a descriptive message:

    git commit -m "Fixed bug in feature X"
    
  3. Push Changes:

    Finally, push the changes to GitHub:

    git push origin master
    

📝 Conclusion

Using Git and GitHub with Django allows you to effectively manage your project, collaborate with others, and keep your code safe. With version control, you can easily track changes, work on new features in branches, and revert to previous versions if something goes wrong.

Remember to frequently commit your changes, push to GitHub, and make use of branches when working on new features. With GitHub, you also have the ability to collaborate with other developers, making it an indispensable tool for professional web development. Happy coding! 🚀