Working with git on Django Projects

1. Initialize Git in Your Django Project

First, you need to set up Git in your project directory. If you haven’t already installed Git, you can download and install it from Git's official site.

Step

Command/Action

Explanation

Open Terminal

Navigate to your project directory using the terminal or command prompt.

cd path_to_your_project

Replace path_to_your_project with the path where your Django project is located.

git init

Initializes a new Git repository in your Django project directory. This will create a .git directory.

git status

Optional: Check the status to see which files are untracked or modified.

2. Create a .gitignore File

It's important to tell Git which files should not be tracked. This typically includes files with sensitive information, like settings.py, and other non-source code files like __pycache__ directories.

Step

Command/Action

Explanation

Create a file named .gitignore in your project root.

This file specifies intentionally untracked files to ignore.

Edit .gitignore

Add the following entries: <br> *.pyc <br> __pycache__/ <br> db.sqlite3 <br> .vscode/ <br> *.env

git add .gitignore

Adds the .gitignore file to staging.

3. Commit Your Changes

After setting up .gitignore, you need to add your project files to the staging area and commit them.

Step

Command/Action

Explanation

git add .

Adds all the files in your project directory, respecting .gitignore, to the staging area.

git commit -m "Initial commit"

Commits the staged files to your repository with a message describing the commit.

4. Push to GitHub

Next, you need to push your repository to GitHub. First, create a repository on GitHub.

Step

Command/Action

Explanation

Go to GitHub and create a new repository

Navigate to http://GitHub.com , sign in, and create a new repository. Do not initialize it with a README, .gitignore, or License.

git remote add origin <repository_URL>

Replace <repository_URL> with your GitHub repository URL to set it as the remote.

git push -u origin master

Pushes your commits to GitHub. -u sets the upstream (default) for future pushes.

5. Collaborate with Others

Allow others to collaborate by cloning your repository, making changes, and pushing updates.

Step

Command/Action

Explanation

They clone the repository

git clone <repository_URL>

They make changes and commit locally

git commit -am "Describe changes"

They push changes

git push origin master

Merge their changes

You pull their changes with git pull and resolve any merge conflicts.

6. Handle Merge Conflicts

If there are conflicts, they must be resolved manually in the conflicting files.

Step

Command/Action

Explanation

Edit files

Open the conflicting files and make the necessary changes to resolve conflicts.

git add <file>

After resolving conflicts in a file, mark it as resolved by adding it to the staging area.

git commit

Commit the resolved changes. Git will insert a default commit message for merges.