Upload your Django Project to GitHub.

Git is a version control system that lets you go back to any version of your code, even if you delete something important by accident. It also lets you keep track of all your work through commits and work with other developers. In this post, I’ll show you how to upload a Django project to Github.

Steps to upload Django project to Github:

  1. Install and setup Git (One-off)
  2. Create a GitHub account (One-off)
  3. git init!
  4. Optional: git status.
  5. Make A Commit.
  6. Create A New Github Repository.
  7. Push To Github Repo.

The first two stages are one-offs, which means you only have to do them once. Therefore the number of steps required to upload a Django project to Github is only five. These are git init, git status, git -A, git commit -m "message", create a GitHub repository and push it to the Github repository.

So let’s get started:

Table of Contents

Step 1: Install and Setup Git (One-off):

Git allows us to run the commands that let us upload a Django project to Github, so it is crucial to have it installed in your operating system. I’ll explain some of the best and most popular methods for installing Git for each major operating system namely Mac, Linux, and Windows.

How To Install Git On Mac.

If you’re on Mac, type the following in your terminal:

$ brew install git

This works because homebrew comes preinstalled in most Mac computers, however, if somehow you do not have it in your Mac computer, you have to install it first. Head over to Git-scm’s Download for MacOS to learn more ways to install Git on your mac computer.

How To Install Git On Linux.

Type in the following command in the terminal of your Linux computer.

$ apt-get install git

Git-scm’s Download for Linux and Unix page offers more ways to download Git for other Linux distrubitions.

How To Install Git On Windows.

For Windows, it is a bit different, the best method to install Git is to use a Git installer.

Here is how to do it:

  1. Go to Git scm’s Downloads for Windows.
  2. Click on the Standalone Installer suitable for your Windows OS.
  3. After the download, run the installer by double-clicking it.
  4. Follow the setup wizard till the end of the installation.

You can also use the winget tool if you have it installed by running the following command:

$ winget install --id Git.Git -e --source winget

To learn more about installing the winget tool on Windows, check out its guide on Using the winget tool to install and manage applications.

Git Setup.

Check if Git has been installed correctly by checking its version.

$ git --version

Which should return the version of Git installed on your computer.

Now let’s set up Git.

Open your terminal and run the following commands one by one.

$ git config --global user.name "Your Name"
$ git config --global user.email "Your email Address"

The first command sets the name that is going to be used for other Git operations we will do. So does your email.

If you wish to change either of these credentials, just rerun the corresponding command with the new credential.

We are done Installing and setting up Git on our computer, it is time to create a GitHub account if you haven’t created one.

Step 2: Create a Github account (One-off).

Follow the steps below to create a GitHub account:

  1. Go to Github’s homepage.
  2. Click the ‘SignUp’ button.
  3. Enter your Email, Username, and Password.
  4. Verify the Human Test.
  5. Click the Create Account button.
  6. Verify your Email.
  7. Answer a few polls.

And we are In!

Before You Upload A Django Project To Github

1. Hide the Sensitive Information of your Django Project using Environment Variables

There is some information that you are not supposed to deploy on Git such as passwords, secret keys, usernames, etc. You can hide them using environment variables.

2. Create a .gitignore file

A .gitignore file lists all the other files and folders in your Django project that are not supposed to be uploaded to Git. These files and folders may be containing sensitive information about your project (like secret keys and passwords) or they are simply not necessary to include in your repository (like virtual environment folders).

Check out DjangoWaves’s guide on Gitignore for a Django project to learn all the folders that you are supposed to include in your .gitignore file.

3. Create a requirements.txt file

In your Django project root folder, there should be a text file that lists all the dependencies of the project and their versions. This is done so that when some other developers want to work on your project, they know the dependencies that are required to run it. In Django, or Python is general, it is a best practice to call this file requirements.txt. You can create it automatically by running the following command:

pip freeze > requirements.txt

This will automatically create a file called requirements.txt, which contains a list of your project’s dependencies and their versions, at the root of your project. Make sure you don’t include it in the .gitignore file.

Step 3: Git Init!

Assuming that you already have the Django project created and are ready to deploy it to Github, cd into the project’s folder (that contains manage.py) Run the following command to initialize the folder as a local Git repository.

$ git init

Step 4 : Optional: Git Status.

The following command is used to check the status of your local git repository, if you run this command on a new Django project that has not been committed to git, it should return something like below.

$ git status

You are recommended to run this command to check if there are any changes to your Django code that need to be pushed to git, if there are any, this command will also give you a better idea of how you should name the commit as it lists out the current changes.

After checking the status, it is time to add the code to a commit.

Step 5: Make A Commit.

Now that the folder is a local git repository, you have to update it.

Run the following commands in that same terminal:

$  git add -A
$ git commit -m "initial commit"

The -A flag is for all, and the -m flag for message. The first commits you add to a repository are usually called ‘initial commit'(s). The next time you run the next command again in that same folder (git commit -m “message”) you have to change the message to cater to the most recent changes you made to the code.

Step 6: Create A New Github Repository.

  1. Click the ‘+’ dropdown at the top right corner of your GitHub dashboard and click on the ‘New Repository’ option.
  2. Type the name of the Repository. It is best practice to give it the same name as that of your Django project.
  3. Give a description of the project if you wish to do so.
  4. Choose if it is going to be a Private or Public directory.
  5. The next step ‘Initialize your project with:’ is optional, skip it if you wish to.
  6. Click the ‘Create Reposity’ button.

Step 7: Push To Github Repo.

For now, what we have done is initiated a folder in our computer to work as a local repository and then we created the actual Github repository to which we want the code to be uploaded. On the page that Github redirects you to after the creation of a new repository, we will use the second option that says ‘…or push an existing repository from the command line.’ This is because we have already created the repository through Github itself. We could have used the first option if that was not the case.

The code we are going to use looks something like this.

$ git remote add origin https://github.com/username/repository-name.git
$ git branch -M main
$ git push -u origin main

Conclusion: How to upload Django project to Github.

That was how to upload a Django project to Github. As you can see the first 2 steps are one-offs meaning you only have to do them once. The next steps you have to repeat every time you are uploading a Django project to Github. This leaves us with only 5 steps to upload a Django project to Gihub. Which are git init, git status, git -A, git commit -m "message", Github repository creation, and Github repository push. All Done

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 125

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *