How to push an existing project to GitHub using Git

A common approach to utilizing GitHub as a project management tool is to establish a repository for the project at the outset, even before commencing any coding. Modifications are proposed using Git as the project progresses, and pull requests are conducted to assess the changes.

Suppose a developer possesses a source code presently stored on their computer. In that case, is there a method for transferring it to GitHub using Git?

Indeed, it is possible, and here are the steps to accomplish it.

Steps to push the code to GitHub

To transfer code from a local repository to GitHub, Git can be utilized. This procedure can be executed by typing Git commands directly into the terminal. Below are the step-by-step instructions on how to do so.

  1. Create a new GitHub repository that will hold the code.

Note: Errors can be avoided by refraining from initializing the repository with a README, LICENSE, or gitignore file.

  1. Open the terminal and navigate to the locally stored project.

  2. Initialize the project as a Git repository using the following command.

git init
  1. Add all the files to the stage using the command mentioned below.
git add .
  1. Use the following command to commit the code with a commit message.
git commit -m "Any Commit message or info."
  1. Navigate to the GitHub repository that was previously created. At the top of the repository is the “Quick setup” tab. Click the copy icon to copy the remote repository URL.
  1. The following command instructs Git to add the URL of the default remote server for this repository.
git remote add origin <linkToEmptyRepo>
  1. Use the following command to set up the repository to utilize the main branch as the primary branch.
git branch -m main
  1. Finally, push the changes to the GitHub repository.
git push -u origin main

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved