Total Pageviews

4504

How to Push your Django Project to GitHub

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

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.


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


…or push an existing repository from the command line

git remote add origin https://github.com/Tetraloops/MyProject.git
git branch -M main
git push -u origin main

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.

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

Instead of removing and re-adding, you can do this:

>git remote set-url origin git://new.url.here

To remove remote use this:

>git remote remove origin
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.

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


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


Cloning a repository

  • On GitHub, navigate to the main page of the repository.
  • Above the list of files, click Code.



Copy the URL for the repository.

  • To clone the repository using HTTPS, under "HTTPS", click .
  • To clone the repository using an SSH key, including a certificate issued by your organization's SSH certificate authority, click SSH, then click .
  • To clone a repository using GitHub CLI, click GitHub CLI, then click .

  • Open Git Bash.
  • Change the current working directory to the location where you want the cloned directory.
  • Type git clone, and then paste the URL you copied earlier.

    git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

  • Press Enter to create your local clone.

$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
> Cloning into `Spoon-Knife`...
> remote: Counting objects: 10, done.
> remote: Compressing objects: 100% (8/8), done.
> remove: Total 10 (delta 1), reused 10 (delta 1)
> Unpacking objects: 100% (10/10), done.

No comments:

Post a Comment

ORA-00845: MEMORY_TARGET not supported on this system

 The shared memory file system should have enough space to accommodate the MEMORY_TARGET and MEMORY_MAX_TARGET values. To verify: SQL> sh...