Open In App

How to Push your Django Project to GitHub

Last Updated : 05 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we have explained how to push a Django project on GitHub using Git and direct upload. Git is a version control system that allows us to track our changes and GitHub is a cloud-based platform where we can store, share, and manage your code or projects. With Git and GitHub we can collaborate with other developers and let others contribute to our project. Moreover, we can work on new features/versions and move back to the previous version if anything goes wrong. Each of the code versions will have a description and user, so we can check who made the changes and when.

PushyourDjangoProjecttoGitHubez

Create a Django Project

Let's start by creating a simple Django project. Later in the article, we will add new features to our project and push the changes to GitHub. Before we create the code, we need a virtual environment to separate the project dependencies from other projects. We have used the venv module to create a virtual environment and activate it for the project.

> python -m venv environment
> environment\Scripts\activate
Screenshot-2024-08-05-134208
Create and activate a virtual environment


Now, install Django in the activated environment. After successful installation, we can check the Django version like this using the djang-admin --version command.

> pip install django
> django-admin --version
5.0.7

As Django is now installed, we can use the startproject command to create a Django project.

django-admin startproject test_project .

The dot(.) at the last ensures that the files are extracted in the current directory.

Screenshot-2024-08-05-142506
Django project Structure

Before we start making changes, we will first set up Git and GitHub for our project.

Installing and Configuring Git

Git is a version control system and we can install Git by downloading the build for its "https://git-scm.com/download/win".

After successful installation, we need to configure git in our system. To configure git, we can use our windows terminal as well as git bash. We will configure the username that we used to create account on GitHub.

> git config --gobal user.name <your username>

Now, we will configure our email used to create GitHub account.

> git config --global user.email <your email>

Now, we are ready to use git in our project.

Initializing git in our Django Project

Initializing git in our project refers to a step to tell git to keep track of the changes we made. We can initialize git in our project by the following command:

> git init

With this command git creates a .git folder

Screenshot-2024-08-05-145504
.git folder

This folder is managed by git itself, and we never need to worry about it. Git uses this folder to keep track of the changes, code versions, commit histories, etc.

Screenshot-2024-08-05-150120
Git Initialization

Git uses special symbols to represent, changes and addition of files. U for new file and M for modification in a file.

Staging and Committing Changes

Git keep tracks of files in three stages: modified, staged, and committed. Modified staged is the first of three, in this stage we make changes to our files. Staging a file is a way to mark our modified file to its current version and making ready to commit our changes. The changes in the staged files will be reflected in the next commit. Commit is the last stage, after committing the changes we can push our changes to GitHub.

We can stage all files in one go or specific file at a time with the 'git add' command:

> git add .

Here, dot (.) is used to stage all modified files.

If we want to stage a single file, we can pass the filename to the 'git add' command:

> git add <filename>

The next step if to commit our changes with a brief commit message. The commit message/description tells other about the changes we have made to the files. It makes easier for other contributors to stay updated on the changes.

We can commit our changes using the following command:

> git commit -m "First commit"

Here, -m flag allows us to add commit message in the single line. Now, we are good to push our code to a GitHub Repository.

Push Django Project to GitHub

To push our code on GitHub, we need a GitHub account. You can refer to this article to create your GitHub account: Introduction to GitHub

After registration, we need to create a repository that we will use to store our code. On the GitHub website, we have a option to create repository. While creating a new repository, we can add an unique name, description, make the repository public or private, etc. A public repository is a repository accessible by everyone and it is the best way to show your projects to others. However, a private repository is repository accessible by the owner and team members. For clients or organization projects, always keep your code private.

Screenshot-2024-08-05-154917
Create a GitHub repo

In the next window, we get a few commands that we can use to link our local repository to GitHub repository.

Screenshot-2024-08-05-155118
GitHub Commands

We have already run a few git commands, now we only need to run commands highlighted in the red box. The command displayed here will be different as your username and project name may differ.

> git remote add origin https://github.com/arunkumar02042002/test-project.git

The above command is used to create a link to our local and remote repository.

> git branch -M main

The above command is used to change the default branch name 'master' to 'main'. Till here, we cannot see our code in the GitHub repository.

> git push origin main

The above command pushes our code the origin's main branch. At this step, GitHub will try to authenticate us and upon successful authentication, the code is pushed to the GitHub repo. Once, we refresh the page, we can see our code here.

Screenshot-2024-08-05-160541
GitHub

Updating and Pushing Changes

We will now add the requirement files containing the dependencies of our project. One can easily create a virtual environment and install all the dependencies required for the project.

> pip freeze > requirements.txt

The above command will add the dependencies to the requirements.txt file.

Now, we the changes to the staging area and push our changes.

> git add .
> git commit -m "Added requirements.txt"
> git push
Screenshot-2024-08-05-163208
GitHub Repository


If we got the remote repository, we can see our new commit and changes.


Next Article
Article Tags :
Practice Tags :

Similar Reads