How to Push an Existing Project on GitHub | Pushing Code to GitHub

Learn how to add existing source code or repositories to GitHub from the command line using GitHub CLI or Git Commands.

How to Push an Existing Project on GitHub | Pushing Code to GitHub

What You'll Learn in this Tutorial

  1. What is GitHub and why you have to use it
  2. Setting up and creating a GitHub repository 
  3. Pushing your project to the new repository

GitHub is a cloud-hosted Git management tool. It is used for your project version control and codebase hosting. GitHub uses Git, a widely-used version control system. GitLab and Bitbucket are similar tools.

If you're looking for a piece of very clear information about GitHub then you're at the right place. keep reading my article. In this tutorial, I will show you the complete process of pushing code on GitHub. 

Prerequisites

To follow this tutorial, a few things are required:

  1. Basic knowledge of Git
  2. Git installed on your system
  3. A GitHub account

With these items in place, we can start the tutorial.

Step 1 — Create a new GitHub Repo

Sign in to GitHub and create a new empty repo. You can choose to either initialize a README or not. It doesn’t really matter because we’re just going to override everything in this remote repository anyways.

Step 2 — Initialize Git in the project folder

From your terminal, run the following commands after navigating to the folder you would like to add.

Initialize the Git Repo

Make sure you are in the root directory of the project you want to push to GitHub and run:

git init

This step creates a hidden .git directory in your project folder, which the git software recognizes and uses to store all the metadata and version history for the project.

git add -A

The git add the command is used to tell git which files to include in a commit and the -A (or --all) argument means “include all”.

Commit Added Files

git commit -m 'Added my project'

The git commit the command creates a new commit with all files that have been “added”. The -m (or --message) sets the message that will be included alongside the commit, used for future reference to understand the commit. In this case, the message is: 'Added my project'.

Add a new remote origin

git remote add origin git@github.com:kushkrg/my-new-project.git

In git, a “remote” refers to a remote version of the same repository, which is typically on a server somewhere (in this case, GitHub). “origin” is the default name git gives to a remote server (you can have multiple remotes) so git remote add origin is instructing git to add the URL of the default remote server for this repo.

Push to GitHub

git push -u -f origin main

The -u (or --set-upstream) flag sets the remote origin as the upstream reference. This allows you to later perform git push and git pull commands without having to specify an origin since we always want GitHub in this case.

The -f (or --force) flag stands for force. This will automatically overwrite everything in the remote directory. We’re using it here to overwrite the default README that GitHub automatically initialized.

All together

git init
git add -A
git commit -m 'Added my project'
git remote add origin git@github.com:sammy/my-new-project.git
git push -u -f origin main