// Tutorial //

How to Push an Existing Project to GitHub

Published on September 21, 2020 · Updated on July 25, 2022
Default avatar

By Nicholas Cerminara

How to Push an Existing Project to GitHub

Introduction

GitHub is a cloud-hosted Git management tool. Git is distributed version control, meaning the entire repository and history lives wherever you put it. People tend to use GitHub in their business or development workflow as a managed hosting solution for backups of their repositories. GitHub takes this even further by letting you connect with coworkers, friends, organizations, and more.

In this tutorial, you will learn how to take an existing project you are working on and push it, so it also exists on GitHub.

Prerequisites

To initialize the repo and push it to GitHub you’ll need:

  1. A free GitHub Account
  2. git installed on your local machine

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.

Screenshot of the user interface to create a new repository on GitHub.

Warning: Through the rest of this tutorial, we’ll assume your GitHub username is sammy and the repo you created is named my-new-project. It is important that you replace these placeholders with your actual username and repo name.

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:

Note: If you already have an initialized Git repository, you can skip this command.

  1. 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.

Add the files to Git index

  1. git add -A

The git add 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

  1. git commit -m 'Added my project'

The git commit 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

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

Note: Remember, you will need to replace the highlighted parts of the username and repo name with your own username and repo name.

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

  1. 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.

Note: If you did not include the default README when creating the project on GitHub, the -f flag isn’t really necessary.

All together

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

Conclusion

Now you are all set to track your code changes remotely in GitHub! As a next step, here’s a complete guide to how to use git.

Once you start collaborating with others on the project, you’ll want to know how to create a pull request.

Want to deploy your application quickly? Try Cloudways, the #1 managed hosting provider for small-to-medium businesses, agencies, and developers - for free. DigitalOcean and Cloudways together will give you a reliable, scalable, and hassle-free managed hosting experience with anytime support that makes all your hosting worries a thing of the past. Start with $100 in free credits!

Learn more here


About the authors
Default avatar
Nicholas Cerminara

author


Default avatar
Bradley Kouchi

editor


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thanks for the article - its a great writeup to address this common issue. For this to work for me, at least using https github repo paths, I had to add

git branch -M main

after adding my commit message.

It says this:

The authenticity of host ‘github.com (13.234.210.38)’ can’t be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added ‘github.com’ (ED25519) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up